简体   繁体   English

如何在JSOUP中选择带有特定标签的“外部” HTML元素?

[英]How to select an 'outer' HTML element with a certain tag within it in JSOUP?

So, say I have this HTML block here: (this is Java JSOUP BTW) 所以,说我这里有这个HTML块:(这是Java JSOUP BTW)

<div class="menuitem"> <span class="ul">Creamy Broccoli Cheddar Soup</span><img class="icon" alt="Vegetarian">

Right now I have a list of elements (as Elements type) and I want to select the elements, but only ones that have an attribute as "Vegetarian" in the alt="" part of the img tag within it. 现在,我有一个元素列表(作为Elements类型),我想选择元素,但是只有那些在img标签的alt =“”部分中具有“ Vegetarian”属性的元素。

Right now I can select based on if it's Vegetarian for example by doing 现在我可以根据是否素食来选择

meals.select("img[alt=vegetarian]") meals.select(“ img [alt =素食主义者”“)

but this only returns the actual img tags as elements, thus losing the name of the object (which is Creamy Broccoli Cheddar Soup in the above example) 但这只会返回实际的img标签作为元素,从而丢失对象的名称(在上面的示例中为Creamy Broccoli Cheddar Soup)

How do I select the div part of the element but still make sure the img tag within has a certain attribute? 如何选择元素的div部分,但仍要确保其中的img标签具有特定属性?

Thanks 谢谢

遍历class menuitem类的所有div元素,并在每次迭代时检查是否meals.select("img[alt=vegetarian]").size() > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM