Xpath没有结果

2024-10-01 15:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着从这个页面http://stats.rleague.com/rl/seas/2014.html得到一个团队和分数的列表,作为学习的练习。你知道吗

我没有得到预期的结果,首先我的进口和网页。你知道吗

In [1]: from lxml import html

In [2]: import requests

In [3]: page = requests.get('http://stats.rleague.com/rl/seas/2014.html')

In [4]: tree = html.fromstring(page.text)

这是标题的html。你知道吗

<html><title>Rugby League Tables / Season 2014</title>

对于团队来说

<tr><td width=20%><a href="../teams/souths/souths_idx.html">Souths</a></td><td width=12%>4t 6g </td><td width=5%> 28</td><td><b>Date:</b>Thu 06-Mar-2014 <b>Venue:</b><a href="../venues/stadium_australia.html">Stadium Australia</a> <b>Crowd:</b>27,282</td></tr>
<tr><td width=20%><a href="../teams/easts/easts_idx.html">Sydney Roosters</a></td><td width=12%>1t 2g </td><td width=5%> 8</td><td><b>Souths</b> won by <b> 20 pts</b>

但是我得到了空白名单,我做错了什么?你知道吗

In [6]: print(tree)
<Element html at 0x7f518067fc78>

In [7]: titles = tree.xpath('//html[@title]/text()')

In [8]: print(titles)
[]

In [11]: teams = tree.xpath('//tr/td[@href]/text()')

In [12]: print(teams)

[]

Tags: textincomtreehttptitlehtmlstats
1条回答
网友
1楼 · 发布于 2024-10-01 15:41:28

更改XPath表达式将得到所需的结果:

# `title` is not an attribute, but a tag.
titles = tree.xpath('.//title/text()')
print(titles)

# `td` does not have `href` attribute, but `a` tag.
teams = tree.xpath('//tr/td/a[@href]/text()')
print(teams)

相关问题 更多 >

    热门问题