如何在python scrapy中使用Xpath提取信息

时间:2017-03-21 17:58:52

标签: python xpath scrapy

我需要一些帮助,用xpath从html页面中提取部分信息!我想提取评论的评级 例如:这是HTML代码

<div id="BVRRRatingOverall_Rating_Summary_1" class="BVRRRating BVRRRatingNormal BVRRRatingOverall">
<div class="BVRRLabel BVRRRatingNormalLabel">Overall rating&nbsp;</div>
<div class="BVRRRatingNormalImage"><img src="/4_9/5/rating.gif" class="BVImgOrSprite" alt="4.9 out of 5" title="4.9 out of 5" width="85" height="16"></div>
<span itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating" class="BVRRRatingNormalOutOf">
      <span itemprop="ratingValue" class="BVRRNumber BVRRRatingNumber">4.9</span>
      <span class="BVRRSeparatorText">out of </span>
      <span itemprop="bestRating" class="BVRRNumber BVRRRatingRangeNumber">5</span>
      <meta itemprop="reviewCount" content="23">
   </span>
</div>

从这段代码中我想显示“4.9中的5”或3个包含“4.9”,“out of”,“5”的变量

我用过这个,但它不起作用:

response.xpath('//div[@class="BVRRRatingNormalImage"]/img/@alt').extract()

任何帮助?

1 个答案:

答案 0 :(得分:0)

response.xpath('//span[@itemprop="aggregateRating"]/span/text()').extract()

它将以列表格式提供三个元素

response.xpath('//span[@itemprop="ratingValue"]/text()').extract_first()

它只会给一个元素作为字符串&#34; 4.9&#34;