我有一个 HTML:
<div class="abc">
<div class="xyz">
<div class="needremove"></div>
<p>text</p>
<p>text</p>
<p>text</p>
<p>text</p>
</div>
</div>
我用过: response.xpath('//div[contains(@class,"abc")]/div[contains(@class,"xyz")]').extract()
结果:
u'['<div class="xyz">
<div class="needremove"></div>
<p>text</p>
<p>text</p>
<p>text</p>
<p>text</p>
</div>']
我想删除<div class="needremove"></div>
。你可以帮我吗?
最佳答案
您可以通过 class="needremove"
获取除 div
之外的所有子标签:
response.xpath('//div[contains(@class, "abc")]/div[contains(@class, "xyz")]/*[local-name() != "div" and not(contains(@class, "needremove"))]').extract()
来自 shell 的演示:
$ scrapy shell index.html
In [1]: response.xpath('//div[contains(@class, "abc")]/div[contains(@class, "xyz")]/*[local-name() != "div" and not(contains(@class, "needremove"))]').extract()
Out[1]: [u'<p>text</p>', u'<p>text</p>', u'<p>text</p>', u'<p>text</p>']
关于python - 使用 python 和 scrapy 删除第一个标签 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30661440/