靓汤4困惑

2024-05-18 14:50:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个小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,所以请容忍我,这将是伟大的,如果你们能帮助我!你知道吗


Tags: textin程序urlforgetallfind
1条回答
网友
1楼 · 发布于 2024-05-18 14:50:00

问题出在线路上

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\u all”属性

相关问题 更多 >

    热门问题