我正在尝试从下面这条代码写成一行的网站上抓取数据统计信息:
<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()
我尝试将data-stat名称添加到属性中,并且仍在返回nonetype对象。
答案 0 :(得分:3)
应该从'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)