![](/img/trans.png)
[英]Beautiful Soup AttributeError: 'NoneType' object has no attribute 'find_all' even though webpage is structured the same
[英]AttributeError: 'NoneType' object has no attribute 'find_all' Python Web Scraping w/ Beautiful Soup
我有兩個問題。 首先,每當我激活這行代碼時,我都會收到標題“AttributeError: 'NoneType' object has no attribute 'find_all'”中列出的錯誤。 其次,我還想在這個特定網站上訪問更多統計信息。 所以,首先,我的代碼如下。 這意味着從網站收集名稱,修剪掉多余的名稱,然后取出這些名稱,將它們插入 URL,並獲取兩個統計信息。 我正在獲取的第一個統計數據位於第 22 行,這是錯誤的來源。 第二個統計數據在 HTML 中,也將在我的代碼之后列出。
import requests
from bs4 import BeautifulSoup
import re
res = requests.get('https://plancke.io/hypixel/guild/name/GBP')
soup = BeautifulSoup(res.text, 'lxml')
memberList = []
skillAverageList = []
for i in soup.select('.playerInfo'):
memberList.append(i.text)
memberList = [e[37:-38] for e in memberList]
members = [re.sub("[A-Z][^A-Z]+$", "", member.split(" ")[1]) for member in memberList]
print(members)
for i in range(len(memberList) + 1):
player = memberList[i]
skyLeaMoe = requests.get('https://sky.lea.moe/stats/' + str(player))
skillAverageList.append(soup.find("div", {"id":"additional_stats_container"}).find_all("div",class_="additional-stat")[-2].get_text(strip=True))
pprint(skillAverageList)
下面是我也想從這個網站上抓取的第二個統計數據(在 HTML 中)。 這個特定的統計數據歸因於這個特定的站點,但上面的代碼有望能夠循環遍歷整個列表( https://sky.lea.moe/stats/Igris/Apple )。
<span class="stat-name">Total Slayer XP: </span> == $0
<span class ="stat-value">457,530</span>
很抱歉,如果這很多,我對 HTML 幾乎一無所知,任何嘗試學習它都是一場斗爭。 在此先感謝任何人。
似乎該站點沒有id
為"additional_stats_container"
的div
,因此soup.find("div", {"id":"additional_stats_container"})
返回 None。
在用瀏覽器檢查這個 URL 的 HTML 時,我找不到這樣的div
。
此腳本將打印所有名稱及其Total Slayer XP
:
import requests
from bs4 import BeautifulSoup
url = 'https://plancke.io/hypixel/guild/name/GBP'
stats_url = 'https://sky.lea.moe/stats/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
for a in soup.select('td a[href*="/hypixel/player/stats/"]'):
s = BeautifulSoup(requests.get(stats_url + a['href'].split('/')[-1]).content, 'html.parser')
total_slayer_xp = s.select_one('span:contains("Total Slayer XP") + span')
print('{:<30} {}'.format(a.text, total_slayer_xp.text if total_slayer_xp else '-'))
印刷:
[MVP+] Igris 457,530
[VIP] Kutta 207,665
[VIP] mistercint 56,455
[MVP+] zouce 1,710,540
viellythedivelon 30
[MVP+] Louis7864 141,670
[VIP] Broadside1138 292,240
[VIP+] Babaloops 40
[VIP+] SparkleDuck9 321,290
[VIP] TooLongOfAUserNa 423,700
...etc.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.