3

我有这个 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 中应该有更快的方法来做到这一点。

4

3 回答 3

4

您可以直接将“a 和 b 之间的文本”翻译成 XPath,如“text()[previous-sibling = a and next-sibling = b]”

IE:

//text()[(preceding-sibling::div[1]/text() = "Special Learning Opportunities") and (following-sibling::div[1]/text() = "Student Services")]

应该管用。

(虽然我测试它失败了,但它似乎是我的 XPath 解释器中的一个错误)

于 2012-08-03T20:00:54.260 回答
2

Here you go, not so classy as the previous answer, but hey - atleast it works! :-)

div[1]//div[contains(text(),"Special Learning Opportunities")]/following-sibling::node()[position() <= count( div[1]//div[contains(text(),"Student Services")]/following-sibling::node()) + 1]
于 2012-08-03T20:10:06.550 回答
1

你可以试试这个。。

//div[contains(text(),"Special Learning Opportunities")]//following-sibling::text()[./following-sibling::div[contains(text(),'Student Services')]]
于 2012-08-06T10:18:11.363 回答