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?
Description. When you are scraping the web pages, you need to extract a certain part of the HTML source by using the mechanism called selectors, achieved by using either XPath or CSS expressions. Selectors are built upon the lxml library, which processes the XML and HTML in Python language.
When you are using text nodes in a XPath string function, then use . (dot) instead of using .//text(), because this produces the collection of text elements called as node-set.
What is a CSS Selector? Essentially, the CSS Selector combines an element selector and a selector value that can identify particular elements on a web page. Like XPath, CSS selector can be used to locate web elements without ID, class, or Name.
response.css('mytag *::text')
The *
will visit all the inner tags of mytag
and ::text
will get the text of each of them
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With