简体   繁体   中英

Beautiful soup is returning a 'NoneType' object, how do I resolve this?

I am trying to scrape the data-stats out of this piece of a website where the below is written as a single line:

<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>

My current code looks like this:

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()

I have tried adding the data-stat names to the attributes and am still returning nonetype objects.

Value should be fetch from 'th' not 'tr' . I have updated the code let me know if works for you.

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

Or Option 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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