gpt4 book ai didi

python - Beautifulsoup 收到 AttributeError : 'NoneType' object has no attribute 'text' from a div's child's text

转载 作者:行者123 更新时间:2023-12-04 08:48:36 25 4
gpt4 key购买 nike

HTML 看起来有点像这样:(还有很多统计信息等,但这是第一个)

<div id="statistics-content">
<div class="statTextGroup">
<div class="statText statText--homeValue">53%</div>
<div class="statText statText--titleValue">Ball Possession</div>
<div class="statText statText--awayValue">47%</div>
</div>
我想提取 53%(如果可能,没有 % 符号)作为主队值。
然后我想提取 47%(如果可能,也没有 % 符号)作为客队值(value)。
我可以稍后添加 Ball Possession 词 - 或者,如果有一种简单的方法可以从该列表中添加 Ball Possession,那么我也会这样做。
所以,我确实尝试过这个:
home_stats = soup.select_one('#statistics-content .statText--homeValue:nth-child(1)').text
我搜索了由此产生的错误:
home_stats = soup.select_one('#statistics-content .statText--homeValue:nth-child(1)').text
AttributeError: 'NoneType' object has no attribute 'text'
但我似乎无法找到适合这种特殊情况的东西。我之前的尝试使用了这种类似的方法,但它们要么在跨度中,要么在标签中,并且具有属性。但这只是包裹在 div 中的文本。
我无法更改 HTML(遗憾的是) - 所以如果可能的话,我需要直接从该 HTML 解析它。
最终结果应如下所示:
[home_team] 拥有 53% 的控球权,[away_team] 拥有 47% 的控球权
主客队都在努力给出正确的名字。我希望“控球权”全部小写,并且我想自己添加 %,因为我想稍后单独使用这些数字。
代码就在那里,只是没有抓取正确的数据。我想检索其他 14 个统计数据,所以希望,如果我做对了这个,那么 nth-child(x) 应该可以工作。
非常感谢 - 这是我开始使用 python 和 webscraping 的旅程,所以感谢所有帮助。
为了方便起见,我现在从这个站点抓取: https://www.scoreboard.com/en/match/SO3Fg7NR/#match-statistics;0

最佳答案

也许这会让你开始。

from bs4 import BeautifulSoup

sample_html = """<div id="statistics-content">
<div class="statTextGroup">
<div class="statText statText--homeValue">53%</div>
<div class="statText statText--titleValue">Ball Possession</div>
<div class="statText statText--awayValue">47%</div>
</div>"""


soup = BeautifulSoup(sample_html, "html.parser")

stats = [
soup.select_one(f'#statistics-content .statText--{value}').getText(strip=True)
for value in ["homeValue", "awayValue"]
]
outcome = "[home_team] had {} ball possession and [away_team] had {} ball possession".format(*stats)
print(outcome)
输出:
[home_team] had 53% ball possession and [away_team] had 47% ball possession
编辑:
谢谢你的网址!事实证明,您的代码没有任何问题。页面内容落后 JS , 所以 BeautifulSoup不会看到。但是,还有其他方法可以获得您想要的东西。
这样做的一种方法是使用 seleniumchrome driver .换句话说,这是一个自动化框架,它允许我们跨各种浏览器执行测试或...抓取网站。
这是我想出的:
import time

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from tabulate import tabulate


options = Options()
options.headless = False
driver = webdriver.Chrome(options=options)

# Use the web browser to access the url
driver.get("https://www.scoreboard.com/en/match/SO3Fg7NR/#match-statistics;0")
# wait for all the elements to load
time.sleep(2)

# Use BS4 to parse the page source obtained by the driver
soup = BeautifulSoup(driver.page_source, "html.parser")

# Close the web browser
driver.close()


# This is used to build a list of text values from all tags
def grab_tags(css_selector: str) -> list:
return [t.getText(strip=True) for t in soup.select(css_selector)]


headers = grab_tags(".detail-experimental .statText--titleValue")
home_team = grab_tags(".detail-experimental .statText--homeValue")
away_team = grab_tags(".detail-experimental .statText--awayValue")

# glue all text items together per stat tab
# A list of tuples -> [(Ball Possesion, 53%, 47%), (Goal Attempts, 10, 5)...]
match_data = list(zip(headers, home_team, away_team))

split_value = 15
# split match data list stat table -> there are 15 items per table -> 3 tables
statistics = [
match_data[i:i+split_value] for i in range(0, len(match_data), split_value)
]

# three stat tables
match, first_half, second_half = statistics

# print any of them
print(tabulate(match, headers=["Stat:", "Home Team", "Away Team"]))

输出:
Stat:              Home Team    Away Team
----------------- ----------- -----------
Ball Possession 53% 47%
Goal Attempts 10 5
Shots on Goal 2 1
Shots off Goal 4 2
Blocked Shots 4 2
Free Kicks 11 10
Corner Kicks 8 2
Offsides 2 1
Goalkeeper Saves 0 2
Fouls 8 10
Yellow Cards 1 0
Total Passes 522 480
Tackles 15 12
Attacks 142 105
Dangerous Attacks 44 29
注意:我确实意识到,如果您正处于网络抓取之旅的开始,这可能会让您大吃一惊,但我已经发表了一些评论来解释正在发生的事情。希望这可以帮助!

关于python - Beautifulsoup 收到 AttributeError : 'NoneType' object has no attribute 'text' from a div's child's text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64183979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com