Sky*_*Fox 11 html python parsing dom scrapy
我有html看起来像这样:
<h1>Text 1</h1>
<div>Some info</div>
<h1>Text 2</h1>
<div>...</div>
Run Code Online (Sandbox Code Playgroud)
我理解如何使用h1中的scrapy信息提取:
content.select("//h1[contains(text(),'Text 1')]/text()").extract()
Run Code Online (Sandbox Code Playgroud)
但我的目标是从中提取内容 <div>Some info</div>
我的问题是我没有关于div的任何具体信息.所有我所知道的,它正好追随<h1>Text 1</h1>.我可以使用选择器在树中获取NEXT元素吗?元素,位于DOM树中的同一级别?
就像是:
a = content.select("//h1[contains(text(),'Text 1')]/text()")
a.next("//div/text()").extract()
Some info
Run Code Online (Sandbox Code Playgroud)
kev*_*kev 16
试试这个xpath:
//h1[contains(text(), 'Text 1')]/following-sibling::div[1]/text()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4790 次 |
| 最近记录: |