我对蟒蛇很陌生。请帮我解决这个错误。
AttributeError: 'NoneType' object has no attribute 'attrs'from bs4 import BeautifulSoup
import urllib2
import requests
url = 'https://www.justdial.com/Pune/Event-Organisers/nct-10194150'
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
page = urllib2.urlopen(req).read()
soup = BeautifulSoup(page,'html.parser')
for h in soup.findAll('h2'):
a = h.find('a')
if 'href' in a.attrs:
l = a.get('href')
print l发布于 2018-03-29 11:39:48
使用“尝试”--除非避免NoneType异常:
from bs4 import BeautifulSoup
import urllib2
import requests
url = 'https://www.justdial.com/Pune/Event-Organisers/nct-10194150'
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
page = urllib2.urlopen(req).read()
soup = BeautifulSoup(page,'html.parser')
for h in soup.findAll('h2'):
a = h.find('a')
try:
if 'href' in a.attrs:
l = a.get('href')
except:
pass
print lOR:检查a是否为None:
from bs4 import BeautifulSoup
import urllib2
import requests
url = 'https://www.justdial.com/Pune/Event-Organisers/nct-10194150'
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
page = urllib2.urlopen(req).read()
soup = BeautifulSoup(page,'html.parser')
for h in soup.findAll('h2'):
a = h.find('a')
if a is not None and 'href' in a.attrs:
l = a.get('href')
print l发布于 2018-03-29 11:34:55
您应该不检查a元素。在a中似乎没有h2标签,因此a就是NoneType。
if a and 'href' in a.attrs:
l = a.get('href')发布于 2018-03-29 11:39:18
您所检查的一些元素为None,您应该确保找到的"a“元素实际上具有attrs属性,您可以使用hasattr内置函数:
hasattr(a, "attrs")如果有吸引,则返回true;如果没有,则返回false。阅读关于这一功能的文章
https://stackoverflow.com/questions/49554956
复制相似问题