nba 结合网络解析:'NoneType' 对象没有属性 'find_all'

nba combine web parsing:"'NoneType' object has no attribute 'find_all'

嗯,我想要 table 播放器和信息,但我 运行 遇到了一个我不明白的问题。

问题出在第8-9行; trs = table.find_all('tr')

from bs4 import BeautifulSoup
import requests

   url = 'https://www.nbadraft.net/2019-nba-draft-combine-measurements'
   resp = requests.get(url)
   soup = BeautifulSoup(resp.content, 'html.parser')
   table = soup.find('div', attrs={'class':'content'}).tbody
   trs = table.find_all('tr')

   for tr in trs:
    tds = tr.find_all('td')
    row = [td.text for td in tds]

---预期--

[['player','info','info',等等]]

---实际---

trs = table.find_all('tr') AttributeError: 'NoneType' 对象没有属性 'find_all'

问题是您首先选择了 <tbody>,这只是一个 header。所有<tr>在下<tbody>。此脚本选择所有行并打印它们。

from bs4 import BeautifulSoup
import requests

url = 'https://www.nbadraft.net/2019-nba-draft-combine-measurements'
resp = requests.get(url)
soup = BeautifulSoup(resp.content, 'lxml')

rows = []
for tr in soup.select('.content table tr'):
    tds = tr.find_all('td')
    rows.append([td.text.strip() for td in tds])

for row in rows:
    print(''.join( '{: <22}'.format(t) for t in row ))

打印:

Player                Height w/o Shoes      Height w/ Shoes       Weight                Wingspan              Standing Reach        Body Fat %            Hand Length           Hand Width            
Nickeil Alexander Walker SG6' 4.25''             6' 5.5''              203.8                 6' 9.5'               8' 6'' '              5.90%                 8.50                  8.75                  
RJ Barrett SF         -                     -                     -                     -                     -                     -                     -                     -                     
Charles Bassey C      6' 8.75''             6' 10''               239.0                 7' 3.5''              9' 1.5''              8.50%                 9.25                  9.50                  
Darius Bazley PF      6' 7.75''             6' 9''                208.4                 7' 0'                 8' 11''               3.60% '               9.00                  9.75                  
Bol Bol C             7' 0.75''             7' 2.5''              208.0                 7' 7''                9' 7.5''              7.10%                 9.25                  9.50                  
Jordan Bone SG        6' 1.5''              6' 2.75''             179.0                 6' 3.25''             7' 11''               5.00%                 7.50                  9.25                  
Brian Bowen II SF     6' 6.25''             6' 7.5''              200.0                 6' 10'                8' 7''                6.50% '               8.50                  9.75                  
Ky Bowman PG          6' 1''                6' 2.25''             181.2                 6' 7''                8' 2''                4.90%                 8.25                  9.00                  
Ignas Brazdeikis SF   6' 5.75''             6' 7.25''             220.8                 6' 9.25''             8' 6''                6.00%                 8.75                  9.50                  
Oshae Brissett SF-PF  6' 7''                6' 8''                203.2                 7' 0''                8' 8''                2.90%                 9.00                  9.50                  
Moses Brown C         7' 1.25''             7' 2.5''              237.2                 7' 4.75''             9' 5''                7.80%                 9.50                  10.25                 
Brandon Clarke PF/C   6' 7.25''             6' 8.25''             207.2                 6' 8.25''             8' 6''                4.90%                 8.25                  9.50                  
Nicolas Claxton C     6' 10''               6' 11.75''            216.6                 7' 2.5''              9' 2''                4.50%                 9.25                  9.50                  

... and so on.