html - XPath - 在两个 DIV 之间选择文本(),该 DIV 由其中匹配的文本标识

标签 html css xpath scrapy

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

最佳答案

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

即:

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

应该可以。

(虽然我测试的时候失败了,但是好像是我的XPath解释器的bug)

关于html - XPath - 在两个 DIV 之间选择文本(),该 DIV 由其中匹配的文本标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802014/

相关文章:

html - 没有折叠 Bootstrap 的侧边栏 3

html - 背景颜色不适用于 <body>

java - element.getText() 方法在 java selenium 中不起作用

python - 使用 XPath 和 python scraper 无法获得正确的结果

javascript - jQuery 和 HTML5 标签?

html - 力求垂直对齐元素框的边框?

html - Mailto:准则和字符限制

javascript - CSS 未应用于插入的表数据

html - 低效的 CSS 选择器建议

xml - 我可以在 xsl for-each 中使用 'and' 运算符吗?