【问题标题】:Scraping text between elements in Scrapy在 Scrapy 中的元素之间抓取文本
【发布时间】:2021-01-20 00:34:30
【问题描述】:

我正在使用 Scrapy,我正在尝试抓取类似这样的内容:

<html>
   <div class='hello'>
    some elements
     .
     .
     .
   </div>
   <div class='hi there'>
       <div>
          <h3> title </h3>
          <h4> another title </h4>
          <p> some text ..... </p>
           "some text without any tag"
          <div class='article'>
            some elements
           . 
           .
          </div>
          <div class='article'>
            some elements
           . 
           .
          </div>
          <div class='article'>
            some elements
           . 
           .
          </div>
       </div>
     </div>
</html>

如果我想从类名为“hi there”的 div 下和类名为“article”的 div 之前的所有元素中提取文本,是否有任何可能的方式使用 XPath 或 CSS 选择器?

【问题讨论】:

    标签: python xpath scrapy css-selectors


    【解决方案1】:

    从未使用过 Scrapy。

    不知道它有什么功能,但是,

    //div[@class='hi there']/div/(div[@class='article'])[1]/preceding-sibling::*
    

    在具有“article”类的 div 之前挑选出元素,并且,

    //div[@class='hi there']/div/(div[@class='article'])[1]/preceding-sibling::text()
    

    在文章 div 之前为您提供内部文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多