In this function I'm parsing the page of the item in the online shop. Some items lack the picture, some lack price etc, so there are few if-else
checks.
My questions are:
- How to get rid of these ugly
if-else
checks? - How I can "pythonize" my code further?
Is it correct? I.e when I manipulate the variable value using previously assigned value:
name = soup.find('span',{"class":"project_name"}) if name is not None: name = re.sub(r'[«,»]','', name.text)
Below is my full code:
def grab_properties(url,html):
soup = BeautifulSoup(html)
link = soup.find('iframe', {"src":"e.smth.com"})
if link is not None:
link = link.get("src")
id = re.match(".*\[(\w+)\].*", link).group(1)
if id is not None:
if id not in ids:
name = soup.find('span',{"class":"project_name"})
if name is not None:
name = re.sub(r'[«,»]','', name.text)
if (soup.find('img',{"id":"item0image"}) is not None):
img_url = soup.find('img',{"id":"item0image"}).get('src')
price = soup.find('span',{"class":"project_price"})
if price is not None:
price = price.text
price = re.sub(u'[руб,\., ]','', price)
else:
price =u"Call us!"
return [id.encode('utf8'), name.encode('utf8'), url.encode('utf8'), img_url.encode('utf8'), price.encode('utf8')]
ids
. Am I right ? \$\endgroup\$ – SylvainD May 6 '14 at 10:14