我有这个 HTML,
<div id="General" class="detailOn">
<div class="tabconstraint"></div>
<div id="InstitutionMain" class="detailseparate">
<div id="InstitutionMain_divINFORight" style="float:right;width:40%"></div>
<div style="font-weight:bold;padding-top:6px">Special Learning Opportunities</div>
Distance learning opportunities<br>
<div style="font-weight:bold;padding-top:6px">Student Services</div>
Remedial services<br>
Academic/career counseling service<br>
<div style="font-weight:bold;padding-top:6px">Credit Accepted</div>
Dual credit<br>
Credit for life experiences<br>
</div>
</div>
我要提取
text() = between [Div/text() = "Special Learning Opportunities</div>
Distance learning opportunities"] and [div/text()="Student Services"]
其他 div 类似
我尝试了这段代码,它为我提供了标识的 div 后面的所有文本,
div[1]/div[contains(text(),"Special Learning Opportunities")]/following-sibling::text()
虽然这段代码给了我 div 之前的所有文本
div[1]/div[contains(text(),"Student Services")]/preceding-sibling::text()
有没有办法准确获取指定 DIV 之间的所有文本。提前致谢。
我正在使用 python 2.x 和 scrapy 进行爬行。
注意:我当前的方法:- 使用这三个 xpath
item['SLO']=site.select('div[1]/div[contains(text(),"Special Learning Opportunities")]/following-sibling::text()').extract()
item['SS']=site.select('div[1]/div[contains(text(),"Student Services")]/following-sibling::text()').extract()
item['CA']=site.select('div[1]/div[contains(text(),"Credit Accepted")]/following-sibling::text()').extract()
我得到三个这样的项目,
item['SLO']=['Distance learning opportunities','Remedial services',' Academic/career counseling service','Dual credit','Credit for life experiences']
item['SS']=['Remedial services',' Academic/career counseling service','Dual credit','Credit for life experiences']
item['CA']=['Dual credit','Credit for life experiences']
然后我在 python 列表上工作以获得我想要的,
但我认为在 XPath 中应该有更快的方法来做到这一点。