Dynelight
Dynelight

Reputation: 2162

Handling exception with BeautifulSoup

I am parsing several individual pages with BeautifulSoup.

Several pages get parsed and it gets to a point, on a specific site, that the parsing fails.

I am looking to extract the following tag:

<span class="black20b">$27.99</span>

I use the following selector to get the tag. As a matter of fact, I tried two different ones and got the same results:

price = individual_page.find("span", {"class","black20b"})
price = ''.join(price.findAll(text=True))  

I decided to print the outcome of price BEFORE the join is executed, on several files and this is the outcome:

...
Downloading: File...
<span class="black20b">$7.79</span>

Downloading: File...
<span class="black20b">$27.99</span>

Downloading: File...
None

The last selector returns "None". Checking on that specific case, it turns out that span is coloured differently for some specific reasons. In other words, that tag does not exist.

How can I handle that exception?

Upvotes: 0

Views: 937

Answers (1)

TerryA
TerryA

Reputation: 59974

To avoid calling the function if the result is None:

price = individual_page.find("span", {"class","black20b"})
if price:
    print ''.join(price.findAll(text=True)) 

Upvotes: 2

Related Questions