问题
在解析文档并找到一些元素之后,您将需要获取这些元素中的数据。
解
- 要获取属性的值,请使用该
[Node.attr(String key)](https://jsoup.org/apidocs/org/jsoup/nodes/Node.html#attr-java.lang.String-)方法 - 对于元素(及其组合子元素)上的文本,请使用
[Element.text()](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#text--) - 对于HTML,使用
[Element.html()](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html--)或[Node.outerHtml()](https://jsoup.org/apidocs/org/jsoup/nodes/Node.html#outerHtml--)适当
例如:
String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";Document doc = Jsoup.parse(html);Element link = doc.select("a").first();String text = doc.body().text(); // "An example link"String linkHref = link.attr("href"); // "http://example.com/"String linkText = link.text(); // "example""String linkOuterH = link.outerHtml();// "<a href="http://example.com"><b>example</b></a>"String linkInnerH = link.html(); // "<b>example</b>"
描述
上述方法是元素数据访问方法的核心。还有其他人:
[Element.id()](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#id--)[Element.tagName()](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tagName--)[Element.className()](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#className--)和[Element.hasClass(String className)](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#hasClass-java.lang.String-)
也可以看看
- 参考文档
[Element](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html)和集合[Elements](https://jsoup.org/apidocs/org/jsoup/select/Elements.html)类 - 使用URL
- 使用CSS选择器语法查找元素
