-1

I'm trying to web scraping.

using BeautifulSoup, i'm parsing several objects and confronted with a problem.

The problem is, i can't predict where the error occur using "find" method.

it looks like

a = soup.find(something).find(something)
b = soup.find(something)
...
f = soup.find(something).find(something)

among a,b,c,d,e,f, error can happen anywhere and their code should not be revised.

the variable 'b' is not matter because it would be None, which is ok.

but variable 'a' or 'f' is problem, because it would end the script.

i know there is "try - exception" grammar but that's not what i want. of course it can be written like this:

try : 
 a = soup.find(something).find(something)
except Exception:
 a = None
...
try : 
 f = soup.find(something).find(something)
except Exception:
 f = None

looks ridiculous, doesnt it?

how can i handle it gracefully?

Yacine Mahdid
  • 723
  • 5
  • 17
cjlee
  • 378
  • 3
  • 10
  • 1
    The problem is that you are chaining two function call that are not guaranteed to find whatever you need. What I would do is check if it's possible to combine the `soup.find(something).find(something)` into `soup.find(something)`. – Yacine Mahdid May 30 '20 at 19:26
  • @YacineMahdid you're right. but the second 'find' method has no attribute to distinguished. maybe i need to make it into 2 steps. thank you anyway – cjlee May 30 '20 at 20:16

3 Answers3

0

I hope this solves your issue here. Just add pass in except statement

except:
    pass
thealpha93
  • 780
  • 4
  • 15
  • FYI--`except: pass` is [considered an anti-pattern](https://realpython.com/the-most-diabolical-python-antipattern/) – DarrylG May 30 '20 at 19:46
  • Sorry but that way is not what i want. if error occur while assigning a, then rest variables(b,c,d,e,f) would not be defined, right? – cjlee May 30 '20 at 20:13
0

in your case you should use:

try:
    f = soup.find(something).find(something)
except AttributeError:
    f = None
0

Would breaking this up into two steps solve the problem?

Something like:

a = soup.find(something)
if a is not None:
    a = a.find(something)

If not, I agree with the AttributeError suggestion.

You may find this question about exception handling interesting/helpful.

maryanne
  • 109
  • 4