【问题标题】:How to get the text from this element with Scrapy ? :: text is not working如何使用 Scrapy 从该元素中获取文本? :: 文字无效
【发布时间】:2022-01-21 17:02:04
【问题描述】:

还有哪些其他方法可以用于元素中的文本?

>>> products.css('h2.entry-title').get()
'<h2 class="entry-title" itemprop="headline"><a href="https://example.com/index.php/2021/12/12/your-20-with-few-clicks-from-stash/" rel="bookmark">Your $20 With Few Clicks From Stash</a></h2>'

但试图获取文本,Your $20 With Fever Clicks From Stash using

products.css('h2.entry-title::text').get()

>>> products.css('h2.entry-title::text').get()
>>> 

不工作。有什么建议吗?谢谢。

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    其实,想要的文本节点Your $20 With Few Clicks From Stasha tag下面。要获得正确的输出,css 表达式如下:

    products.css('h2.entry-title a::text').get().strip()
    

    在scrapy shell中的实现:

    In [6]: from scrapy.selector import Selector
    
    In [7]: %paste
    html_doc="""
    <html>
     <body>
      <h2 class="entry-title" itemprop="headline">
       <a href="https://example.com/index.php/2021/12/12/your-20-with-few-clicks-from-stash/" rel="bookmark">       
        Your $20 With Few Clicks From Stash
       </a>
      </h2>
     </body>
    </html>
    """
    
    ## -- End pasted text --
    
    In [8]: sel = Selector(text=html_doc)
    
    In [9]: sel.css('h2.entry-title a::text').get().strip()
    Out[9]: 'Your $20 With Few Clicks From Stash'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 2020-05-29
      • 2015-12-16
      • 1970-01-01
      • 2020-08-25
      • 2016-12-12
      相关资源
      最近更新 更多