Scrapy - Get all data within selector

霸气de小男生 提交于 2019-12-23 05:32:05

问题


If I have some HTML in the response that looks like:

<body>
    Body text
    <div>
        Div text
    </div>
</body>

If I do response.xpath('//body/text()').extract() I will only get [Body text]

I want to get everything inside <body> including the tags i.e. this whole thing:

Body text
<div>
    Div text
</div>

How can I accomplish that?

Thank you.


回答1:


Try it:

response.xpath('//body/node()/text()')

Or if you want the tags too:

response.xpath('//body/node()')



回答2:


Try

//body/(descendant::text() | following::text())

or

//body/descendant::text() | //body/following::text()


来源:https://stackoverflow.com/questions/31586271/scrapy-get-all-data-within-selector

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