你们知道如何获取所有 img 标签,但使用 xpath 在具有页脚 id 的 div 下排除 img 标签吗?
目前要在 html 页面上获取所有 img 标签,我这样做:
imgs = tree.xpath('//img')
但我想排除一个 div 下的所有 img 标签,其 id 为页脚,所以我正在做这个:
imgs = tree.xpath('//*[not(div[@id="footer"])]//img')
<-但这不起作用
你们知道如何获取所有 img 标签,但使用 xpath 在具有页脚 id 的 div 下排除 img 标签吗?
目前要在 html 页面上获取所有 img 标签,我这样做:
imgs = tree.xpath('//img')
但我想排除一个 div 下的所有 img 标签,其 id 为页脚,所以我正在做这个:
imgs = tree.xpath('//*[not(div[@id="footer"])]//img')
<-但这不起作用
Should be something like:
imgs = tree.xpath('//img[not(parent::div[@id="footer"])]')
Breakdown:
//img
- search all <img>
tags[]
- where clausenot(parent::div[@id="footer"])
- not has a (direct) parent div with attribute id with value footerIf the <div>
element is not the direct parent of <img>
but one of it's parents, use:
imgs = tree.xpath('//img[not(ancestor::div[@id="footer"])]')