Home > Blockchain >  How to get the text from this element with Scrapy ? :: text is not working
How to get the text from this element with Scrapy ? :: text is not working

Time:12-20

What are some other method can we use for text from element ?

>>> products.css('h2.entry-title').get()
'<h2  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>'

but trying to get the text, Your $20 With Few Clicks From Stash using

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

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

is not working. Any suggestion? Thank you.

CodePudding user response:

Actually, the desired text node Your $20 With Few Clicks From Stash is under a tag. To get the correct output the css expression would be as follows:

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

Implementation in scrapy shell:

In [6]: from scrapy.selector import Selector

In [7]: %paste
html_doc="""
<html>
 <body>
  <h2  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'
  • Related