繁体   English   中英

AttributeError: 'NoneType' object 没有属性'find_all' Python Web 刮花

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM