我试图选择成分清单中的成分,但也有工具提示散落在其中(在BBC好食品网站)。
作为一个精简的例子:
<li class="ingredients-list__item" itemprop="ingredients">
400g
<a href="/glossary/new-potatoes" class="ingredients-list__glossary-link tooltip-processed">
new potato
<div id="gf-tooltip-0" class="gf-tooltip" role="tooltip">
<div class="gf-tooltip__content">
<div class="gf-tooltip__text">
<p>unwanted tooltip</p>
</div>
</div>
</div>
</a>, halved if large
<span class="ingredients-list__glossary-element" id="ingredients-glossary"></span>
</li>
我正试着用'400g new potato, halved if large'
来结束,或者同样好,['400g', 'new potato', ', halved if large']
。
在我尝试过的其他事情中:
s.xpath("//li[@class='ingredients-list__item'][not(div[@class='gf-tooltip'])]//text()").extract()
但是它仍然返回工具提示div中的文本。
发布于 2018-01-07 13:38:35
一种可能的方法是排除文本节点,其中任何一个祖先都是工具提示div
(为了提高可读性,将其分成2行):
//li[@class='ingredients-list__item']
//text()[not(ancestor::div[@class='gf-tooltip'])]
https://stackoverflow.com/questions/48137481
复制相似问题