【发布时间】:2017-04-07 07:08:23
【问题描述】:
这可能是愚蠢的。但我正在尝试编写一个简单的刮板来从该网站获取列表:https://online.ncat.nsw.gov.au/Hearing/HearingList.aspx?LocationCode=2000
好吧,实际上它最终会针对每个 LocationCode 运行,但这是一个示例页面。
我想提取每个日期的<span> 标题和table 数据。
数据的一般形式是:
<span id="lblSubHeader1242017" class="clsGridItem">1:15 PM Wednesday, 12 Apr 2017 at Room 15.6 Level 15, 66 Goulburn st </span>
<hr />
<table id="dg1242017">
<tr class="clsGridItem">
<td width="15%">RT 17/11111</td>
<td width="30%">Name of party</td>
<td width="55%">Name of party</td>
</tr>
...
</table>
这很粗糙,但我可以使用以下形式的代码很好地获取表格数据:
page = requests.get('https://online.ncat.nsw.gov.au/Hearing/HearingList.aspx?LocationCode=2000')
tree = html.fromstring(page.content)
events = tree.xpath('//table//td/text()')
但是当我尝试抓取表格外的跨度时,我可以获得位置和日期信息,例如:
days = tree.xpath('//span[starts-with(@id,"lbl")]/text()')
或
days = tree.xpath('//span[@class,"clsGridItem"]/text()')
我只得到以下两个结果:
days: ['There are no matters listed in SYDNEY today', 'There are no matters listed in SYDNEY today']
这些指的是页面下方约 2/3 处的两个跨度:
<span id="lbl1442017" style="font-weight:bold;">SYDNEY: Friday, 14 Apr 2017</span><br /><br /><span id="lblError1442017" class="clsGridItem">There are no matters listed in SYDNEY today</span><br /><br /><br /><span id="lbl1742017" style="font-weight:bold;">SYDNEY: Monday, 17 Apr 2017</span><br /><br /><span id="lblError1742017" class="clsGridItem">There are no matters listed in SYDNEY today</span>
谁能向我解释我做错了什么?
为什么会跳过其他跨度?
【问题讨论】:
标签: html python-3.x xpath python-requests lxml