Scrapy css selector: get text of all inner tags

北慕城南 提交于 2020-01-10 09:24:32

问题


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?


回答1:


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

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




回答2:


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.


来源:https://stackoverflow.com/questions/40985060/scrapy-css-selector-get-text-of-all-inner-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!