我目前正在写一个Python脚本,其中的一部分是从2005 - 2015年间NBA选秀中每个球员职业生涯的前4个赛季获得的。我已经花了近2个小时来处理这个问题(越来越沮丧),但我一直无法获得单个玩家的赢利份额。我正在尝试使用以下链接中的“高级”表作为测试用例:https://www.basketball-reference.com/players/b/bogutan01.html#advanced::none
在从选秀页面中获取球员的名字时,我没有遇到任何问题,但是我已经尝试了很多次以下代码的迭代,但都没有成功地访问stat所在的td元素。
playerSoup = BeautifulSoup(playerHtml)
playertr = playerSoup.find_all("table", id = "advanced").find("tbody").findAll("tr")
playerws = playertr.findAll("td")[21].getText()
发布于 2019-04-28 08:03:39
此页使用JavaScript
添加表,但它不从服务器读取数据。所有表格都是超文本标记语言,但作为注释<!-- ... ->
使用BeautifulSoup
,你可以找到所有的评论,然后检查哪个评论有文本"Advanced"
。然后,您可以在BeautifulSoup
中将此注释用作普通的HTML语言
import requests
from bs4 import BeautifulSoup
from bs4 import Comment
url = 'https://www.basketball-reference.com/players/b/bogutan01.html#advanced::none'
r = requests.get(url)
soup = BeautifulSoup(r.content)
all_comments = soup.find_all(string=lambda text: isinstance(text, Comment))
for item in all_comments:
if "Advanced" in item:
adv = BeautifulSoup(item)
playertr = adv.find("table", id="advanced")
if not playertr:
#print('skip')
continue # skip comment without table - go back to `for`
playertr = playertr.find("tbody").findAll("tr")
playerws = adv.find_all("td")[21].getText()
print('playertr:', playertr)
print('playerws:', playerws)
for row in playertr:
if row:
print(row.find_all('th')[0].text)
all_td = row.find_all('td')
print([x.text for x in all_td])
print('--')
https://stackoverflow.com/questions/55885722
复制相似问题