简体   繁体   English

如何在jsoup中使用选择器语法获取Attribute

[英]How to get Attribute using selector syntax in jsoup

I need to get value of attribute href of a tag. 我需要得到属性的值hrefa标签。
I know using a.attr("href") I can get href attribute value. 我知道使用a.attr("href")我可以得到href属性值。

But I want to know is there any other way to get href attribute as like in jTidy 但我想知道是否还有其他方法可以像在jTidy那样获取href属性
(using syntax like //a/@href ) for Jsoup. (对于Jsoup使用类似//a/@href语法)。

Means can I use some selector syntax to get attribute directly ? 我可以使用一些选择器语法直接获取属性吗?

Thanks. 谢谢。

No, you cant retrieve the attribute value by a single selector. 不,您无法通过单个选择器检索属性值。 Its purpose is to select elements by various criteria. 其目的是按各种标准选择要素。

But you can select only those elements which have the attribute and then ask it's value. 但是,您只能选择具有该属性的元素,然后询问它的值。

Element withAttr = doc.select("a[href]").first();
String attrAvlue = withAttr.attr("href");

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

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