我可以成功刮取多列,但是,我无法获取相应球员的球队名称。到目前为止,这是我的代码:
from urllib.request import urlopen
from lxml.html import fromstring
import pandas as pd
url = "https://www.basketball-reference.com/leagues/NBA_2018_advanced.html"
content = str(urlopen(url).read())
comment = content.replace("-->","").replace("<!--","")
tree = fromstring(comment)
for idx, bball_row in enumerate(tree.xpath('//table[contains(@class,"stats_table")]//tr[contains(@class,"full_table")]')):
names = bball_row.xpath('.//td[@data-stat="player"]/a')[0].text
mp = bball_row.xpath('.//td[@data-stat="mp"]/text()')[0]
per = bball_row.xpath('.//td[@data-stat="per"]/text()')[0]
ws = bball_row.xpath('.//td[@data-stat="ws"]/text()')[0]
bpm = bball_row.xpath('.//td[@data-stat="bpm"]/text()')[0]
vorp = bball_row.xpath('.//td[@data-stat="vorp"]/text()')[0]
print(names, per, ws, bpm, vorp)
一切到此为止。不过,我想添加团队名称的类别。我正在寻找缩写的团队名称(例如,俄克拉荷马城的OKC)。
以下代码遇到错误:
team = bball_row.xpath('.//td[@data-stat="team_id"]/a')[0].text
print(team)
代码开始打印所有团队名称,然后出错。
这是错误:
team = bball_row.xpath('.//td[@data-stat="team_id"]/a')[0].text
IndexError: list index out of range
只是重申我在寻找什么... 我要尝试将缩写的球队名称添加到相应球员的旁边。
任何建议将不胜感激。我要在此先感谢社区的时间和努力!
答案 0 :(得分:1)
您的脚本仅在找不到所需的值时才抛出该错误。您所能做的就是捕捉错误并以正确的方式进行处理。尝试以下脚本:
import requests
from lxml.html import fromstring
url = "https://www.basketball-reference.com/leagues/NBA_2018_advanced.html"
content = requests.get(url).text
comment = content.replace("-->","").replace("<!--","")
tree = fromstring(comment)
for row in tree.xpath('//table[contains(@class,"stats_table")]//tr[contains(@class,"full_table")]'):
names = row.xpath('.//td[@data-stat="player"]/a')[0].text
mp = row.xpath('.//td[@data-stat="mp"]/text()')[0]
per = row.xpath('.//td[@data-stat="per"]/text()')[0]
ws = row.xpath('.//td[@data-stat="ws"]/text()')[0]
bpm = row.xpath('.//td[@data-stat="bpm"]/text()')[0]
vorp = row.xpath('.//td[@data-stat="vorp"]/text()')[0]
try:
team = row.xpath('.//td[@data-stat="team_id"]/a')[0].text
except IndexError: team = "N/A"
print(names, per, ws, bpm, vorp, team)
您可能会得到的输出:
Alex Abrines 9.0 2.2 -2.2 -0.1 OKC
Quincy Acy 8.2 1.0 -2.2 -0.1 BRK
Steven Adams 20.6 9.7 3.3 3.3 OKC
Bam Adebayo 15.7 4.2 0.2 0.8 MIA