Python : Another 'NoneType' object has no

2019-04-17 00:37发布

问题:

For a newbie exercise , I am trying to find the meta tag in a html file and extract the generator so I did like this :

Version = soup.find("meta", {"name":"generator"})['content']

and since I had this error :

TypeError: 'NoneType' object has no attribute '__getitem__'

I was thinking that working with exception would correct it, so I wrote :

try: Version = soup.find("meta", {"name":"generator"})['content']

except NameError,TypeError:

     print "Not found"

and what I got is the same error.

What should I do then ?

回答1:

The soup.find() method did not find a matching tag, and returned None.

The [...] item access syntax looks for a __getitem__ method, which is the source of the AttributeError here:

>>> None[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'

Test for None explicitly:

Version = soup.find("meta", {"name":"generator"})
if Version is not None:
    Version = Version['content']
else:
    print "Not found"

Your exception handling would work too, provided you use parenthesis to group the exceptions:

try:
    Version = soup.find("meta", {"name":"generator"})['content']
except (NameError, TypeError):
    print "Not found"

Without parenthesis you are telling Python to catch NameError exceptions and assign the resulting exception object to the local name TypeError. This except Exception, name: syntax has been deprecated because it can lead to exactly your situation, where you think you are catching two exceptions.

However, your code here should not throw a NameError exception; that'd be a separate problem better solved by instantiating your variables properly; the following would work just as well here:

try:
    Version = soup.find("meta", {"name":"generator"})['content']
except TypeError:
    # No such meta tag found.
    print "Not found"


回答2:

Try this:

content = None
Version = soup.find("meta", {"name":"generator"})
if Version:
    content = Version.get('content') 
    #or even
    #Version = Version.get('content')
else:
    print "Not found"

The issue is, soup.find returns a None if match was not found, and extracting data out of None results in error.