gpt4 book ai didi

python - Beautiful soup 正在返回一个 'NoneType' 对象,我该如何解决这个问题?

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:11 26 4
gpt4 key购买 nike

我正试图从网站的这个片段中抓取数据统计信息,其中以下内容写成一行:

<tr ><th scope="row" class="right " data-stat="ranker" >1</th><td class="left " data-stat="school_name" ><a href='/cbb/schools/abilene-christian/2019.html'>Abilene Christian</a></td><td class="right " data-stat="g" >26</td><td class="right " data-stat="wins" >21</td><td class="right " data-stat="losses" >5</td><td class="right " data-stat="win_loss_pct" >.808</td><td class="right " data-stat="srs" >-1.73</td><td class="right " data-stat="sos" >-7.32</td><td class="right " data-stat="wins_conf" >10</td><td class="right " data-stat="losses_conf" >3</td><td class="right " data-stat="wins_home" >11</td><td class="right " data-stat="losses_home" >1</td><td class="right " data-stat="wins_visitor" >8</td><td class="right " data-stat="losses_visitor" >4</td><td class="right " data-stat="pts" >1953</td><td class="right " data-stat="opp_pts" >1652</td><td class="right iz" data-stat="x" ></td><td class="right " data-stat="mp" >1050</td><td class="right " data-stat="fg" >705</td><td class="right " data-stat="fga" >1468</td><td class="right " data-stat="fg_pct" >.480</td><td class="right " data-stat="fg3" >189</td><td class="right " data-stat="fg3a" >480</td><td class="right " data-stat="fg3_pct" >.394</td><td class="right " data-stat="ft" >354</td><td class="right " data-stat="fta" >497</td><td class="right " data-stat="ft_pct" >.712</td><td class="right " data-stat="orb" >257</td><td class="right " data-stat="trb" >860</td><td class="right " data-stat="ast" >405</td><td class="right " data-stat="stl" >226</td><td class="right " data-stat="blk" >75</td><td class="right " data-stat="tov" >330</td><td class="right " data-stat="pf" >509</td></tr>

我现在的代码是这样的:

ncaa='https://www.sports-reference.com/cbb/seasons/2019-school-stats.html'

driver.get(ncaa)

soup = BeautifulSoup(driver.page_source,"html")

item = soup.find('tr ', attrs={'class':'right '}).text

print(item)

driver.quit()

我已经尝试将数据统计名称添加到属性中,但仍然返回非类型对象。

最佳答案

值应该从 'th' 而不是 'tr' 获取。我已经更新了代码让我知道它是否适合你。

from selenium import webdriver
from bs4 import BeautifulSoup
driver=webdriver.Chrome()
ncaa='https://www.sports-reference.com/cbb/seasons/2019-school-stats.html'
driver=webdriver.Chrome()
driver.get(ncaa)

soup = BeautifulSoup(driver.page_source,'html.parser')

items = soup.find_all("th", {"class" : "right"})

for item in items:
print(item.text) #It will print all values 1 to 353

或选项 2:

from bs4 import BeautifulSoup
import requests
ncaa='https://www.sports-reference.com/cbb/seasons/2019-school-stats.html'

html = requests.get(ncaa).text

soup = BeautifulSoup(html,'html.parser')

items = soup.find_all("th", {"class" : "right"})

for item in items:
print(item.text)

关于python - Beautiful soup 正在返回一个 'NoneType' 对象,我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753435/

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