我正在开发一个小的python程序来进行网络抓取:
from bs4 import BeautifulSoup
import requests
hero = raw_input("Enter Hero Name: ")
url = "http://www.dotahut.com/heroes/" + hero.lower().replace(' ', '_')
#to double check url in case of error
print (url)
r = requests.get(url)
soup = BeautifulSoup(r.text)
# find 'weak against' heroes
print('\nWeak Against:\n')
for champ in soup.find(class_='weak-block').find_all(class_='champ-block'):
print(champ.find(class_='name').get_text())
# find 'strong against' heroes
print('\nStrong Against:\n')
for champ in soup.find(class_='strong').find_all(class_='champ-block'):
print(champ.find(class_='name').get_text())
当我运行此程序时,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\LewisJames\Google Drive\Personal Files\Programming\Python\Dota 2 Counter.py", line 20, in <module>
for champ in soup.find(class_='weak-block').find_all(class_='champ-block'):
AttributeError: 'NoneType' object has no attribute 'find_all'
阅读完BS4文档并检查网站元素后,我很困惑这是如何工作的。
我是BS4和python的初学者,所以请耐心等待,如果你们能帮帮我的话会很棒!
答案 0 :(得分:0)
问题在于
行for champ in soup.find(class_='weak-block').find_all(class_='champ-block'):
具体来说,
soup.find(class_='weak-block')
返回'None',所以下一步,
find_all(class_='champ-block'):
无需搜索,因此返回AttributeError:'NoneType'对象没有属性'find_all'