【问题标题】:How to extract information with Xpath in python scrapy如何在 python scrapy 中使用 Xpath 提取信息
【发布时间】:2017-03-21 17:58:52
【问题描述】:

我需要一些帮助来使用 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 out of 5”或包含“4.9”、“out of”、“5”的 3 个变量

我用过这个但是没用:

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

有什么帮助吗?

【问题讨论】:

  • 对于这个 html 示例应该可以使用这个 xpath 表达式://img[@class="BVImgOrSprite"]/@alt。但是没有站点链接和您的代码示例,我不知道它是否适合您。

标签: python xpath scrapy


【解决方案1】:
response.xpath('//span[@itemprop="aggregateRating"]/span/text()').extract()

它将以列表格式给出三个元素

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

它只会给出一个元素作为字符串“4.9”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多