【问题标题】:How can I get all text data of a node with xpath in scrapy如何在scrapy中使用xpath获取节点的所有文本数据
【发布时间】:2018-01-18 13:51:01
【问题描述】:

我正在尝试从网站上抓取用户评论数据。我希望最后有 2 列数据(评分和评论)。

这是一个模拟我的抓取问题的示例 xml 文件。我在https://www.freeformatter.com/xpath-tester.html#ad-output.to 上试过了,得到输出。

<root>
  <div class="user-review">
    <div class="rating"> 5,0 </div>
    <p class="review-content"> Reiew text of item/movie.
      <span class="details">
          <span class="details-header">Detail: </span>
      <span class="details-content">Some details to emphasis</span>
      </span>
      Continue to review
    </p>
  </div>
  <div class="user-review">
    <div class="rating"> 4,0 </div>
    <p class="review-content">Reiew text of item/movie.
    </p>
  </div>
  <div class="user-review">
    <div class="rating"> 4,0 </div>
    <p class="review-content">Reiew text of item/movie.
    </p>
  </div>
</root>

我可以通过下面的查询获得 3 个评分值。

/root/div/div[@class="rating"]/text()

输出:

Text=' 5,0 '
Text=' 4,0 '
Text=' 4,0 '

当我尝试获取评论部分时,第一个文本分为 2 个部分。因此,我有两个不同大小的列表(3 个大小的评级和 4 个大小的评论),无法将评论与评级匹配

//p[@class="review-content"]/text()

输出:

Text='  Reiew text of item/movie.
        '
Text='
Continue to review
    '
Text='Reiew text of item/movie.
    '
Text='Reiew text of item/movie.

任何人都可以帮助我获得我的预期输出之一吗?

预期输出1:

Text='  Reiew text of item/movie.
    Continue to review
    '
Text='Reiew text of item/movie.
    '
Text='Reiew text of item/movie.

预期输出2:

Text='  Reiew text of item/movie. Some details to emphasis
    Continue to review
    '
Text='Reiew text of item/movie.
    '
Text='Reiew text of item/movie.

【问题讨论】:

标签: xpath web-scraping scrapy


【解决方案1】:

试试这个,sel 在这里选择器,在你的情况下可能是响应

tags = sel.xpath('//p[@class="review-content"]')
reviews = []
for tag in tags:
    text = " ".join(tag.xpath('.//text()').extract())
    reviews.append(text)

【讨论】:

    【解决方案2】:

    您必须循环使用 user-review 类的 div 元素,并从每个元素中提取评论内容。如果你想要一个单线,看看这个:

    import scrapy
    
    text = """
    <root>
      <div class="user-review">
        <div class="rating"> 5,0 </div>
        <p class="review-content"> Reiew text of item/movie.
          <span class="details">
              <span class="details-header">Detail: </span>
          <span class="details-content">Some details to emphasis</span>
          </span>
          Continue to review
        </p>
      </div>
      <div class="user-review">
        <div class="rating"> 4,0 </div>
        <p class="review-content">Reiew text of item/movie.
        </p>
      </div>
      <div class="user-review">
        <div class="rating"> 4,0 </div>
        <p class="review-content">Reiew text of item/movie.
        </p>
      </div>
    </root>
    """
    
    selector = scrapy.Selector(text=text)
    review_content = [review.xpath('normalize-space(.//p[@class="review-content"])').extract_first() for review in selector.xpath('//div[@class="user-review"]')]
    

    【讨论】:

    • 最后一行代码应该是选择器而不是sel。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2017-02-24
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多