38

I have a tag and I want to get all the text inside available. I am doing this:

response.css('mytag::text')

But it is only getting the text of the current tag, I also want to get the text from all the inner tags.

I know I could do something like:

response.xpath('//mytag//text()')

But I would like to do it with css selectors. How can I achieve this?

2 Answers 2

60
response.css('mytag *::text')

The * will visit all the inner tags of mytag and ::text will get the text of each of them

0
17

Get text of only selected node.

response.css('mytag::text')

Get text of selected node and its child nodes.

response.css('mytag ::text')

See the difference between these two versions. The only difference is the space. If there is no space then only text/attributes of current nodes are returned. If there is space then it selects text/attributes of self and child nodes

response.css('h1 a::attr(href)') # only current node attribute

response.css('h1 ::attr(href)') # current node and all child nodes attribute.
3
  • can you please show where did you get this? It's not working for me
    – cikatomo
    Oct 25, 2020 at 7:28
  • 1
    I use them mostly in scrapy and they work without any issue. Oct 26, 2020 at 11:08
  • Cristal clear. Love 'em. Thanks for saving my day.
    – Randy Lam
    Aug 17, 2022 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.