如何在Xpath查询中创建OR条件

时间:2014-02-13 05:28:24

标签: xpath scrapy

我有两种不同类型的HTML代码:

我的第一个代码是:

<div class="course-general-info hide-for-medium-up">
<!-- TODO: check date format here -->
  <div class="headline"><strong>2014-01-27</strong></div>
  <div class="subline">Course start</div>
  <div class="headline"><strong>2014-04-27</strong></div>
  <div class="subline">Course end</div>
<div class="headline">Basis</div>
<div class="subline">Level</div>
</div>

我的第二个代码是:

<div class="course-general-info hide-for-medium-up">
  <div class="headline">Available Soon</div>
  <div class="subline">Course start</div>
<!-- TODO: check date format here -->
<div class="headline">Basis</div>
<div class="subline">Level</div>
</div>

我需要在单个Xpath查询中获取以下值      2014-01-27 或     即将推出

我的单独xpath查询是:

courseDurationDate = courseDetailData.xpath('//div[@class = "headline"]/strong/text()').extract()

CourseDutaionAvailableSoon = courseDetailData.xpath('//div[@class = "headline"]/text()').extract()

请帮助在Xpath查询中编写或条件。提前谢谢....

3 个答案:

答案 0 :(得分:1)

假设2 divs中只有一个会同时出现,那么应该这样做:

(//div[@class = "headline"]/strong | 
 //div[@class = "headline" and not(strong)])[1]/text()

答案 1 :(得分:0)

单线

div[@class='course-general-info hide-for-medium-up']/div[@class='headline'][1]/descendant::text()

答案 2 :(得分:0)

我尝试使用Xpath Query,它为我工作:

courseDurationDate = courseDetailData.xpath('//div[@class = "course-general-info hide-for-medium-up"]/div[@class = "headline"]/strong/text() | \
                                                 //div[@class = "course-general-info hide-for-medium-up"]/div[@class = "headline" and not (strong)][1]/text()').extract()