不幸的是,我是 XPath 的初学者,并不完全确定它是如何工作的。对于我的一个项目,我正在寻找一种方法来解析 9 列表中的 5 列。这是我到目前为止所做的工作:
url="".join(["http://www.basketball-reference.com/leagues/NBA_2011_games.html"])
#getting the columns 4-7
page=requests.get(url)
tree=html.fromstring(page.content)
# the //text() is because some of the entries are inside <a></a>s
data = tree.xpath('//table[@id="games"]/tbody/tr/td[position()>3 and position()<8]//text()')
所以我的解决方法的想法是,获取另一个仅获取第一列的列表,然后在额外的步骤中将两者组合起来,但这似乎不雅观且不必要。
对于我到目前为止尝试过的 XPath
//table[@id="games"]/tbody/tr/td[position() = 1]/text() | //table[@id="games"]/tbody/tr/td[position()>3 and position()<8]//text()
这也不包括第一列(日期)。 (根据 w3schools)|是连接两个 XPath 语句的运算符。
这是我现在的完整代码。到目前为止,数据将被放入两个列表中。
希望我没有做任何愚蠢的事情,谢谢你的帮助。
from lxml import html
import requests
url="".join(["http://www.basketball-reference.com/leagues/NBA_1952_games.html"])
page=requests.get(url)
tree=html.fromstring(page.content)
reg_data = tree.xpath('//table[@id="games"]/tbody/tr/td[position() = 1]/text() | //table[@id="games"]/tbody/tr/td[position()>3 and position()<8]//text()')
po_data = tree.xpath('//table[@id="games_playoffs"]/tbody/tr/td[position() = 1]/text() | //table[@id="games_playoffs"]/tbody/tr/td[position()>3 and position()<8]//text()')
n=int(len(reg_data)/5)
if int(year) == 2016:
for i in range(0,len(reg_data)):
if len(reg_data[i])>3 and len(reg_data[i+1])>3:
n = int((i)/5)
break
games=[]
for i in range(0,n):
games.append([])
for j in range(0,5):
games[i].append(reg_data[5*i+j])
po_games=[]
m=int(len(po_data)/5)
if year != 2016:
for i in range(0,m):
po_games.append([])
for j in range(0,5):
po_games[i].append(po_data[5*i+j])
print(games)
print(po_games)
最佳答案
看起来很多数据都包含在链接 (a) 标记中,因此当您请求文本节点子节点时,您找不到任何数据,因为您需要更深入一层。
而不是
/text()
做
//text()
两个斜杠表示选择任何级别的后代的 text() 节点。
您还可以将整个表达式组合成
//table[@id="games"]/tbody/tr/td[position() = 1 or (position()>3 and position()<8)]//text()
而不是有两个表达式。
我们甚至可以进一步缩短到
//table[@id="games"]//td[position() = 1 or (position()>3 and position()<8)]//text()
但是这个表达式存在风险,因为它会选取表中任何位置出现的 td 元素(前提是它们是第一、第四、第五、第六或第七列),而不仅仅是正文中的行。但是,在您的目标中,这将起作用。
另请注意,像 [position()=1]
这样的表达式并不是必需的。您可以将其缩短为[1]
。仅当您需要上下文节点以外的节点的位置,或者需要编写更复杂的选择(就像我们需要多个特定索引时所做的那样)时,才需要位置函数。
关于python xpath 表的一些但不是所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34601796/