Python : Another 'NoneType' object has no attribute error

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!