從元素提取屬性、文本和HTML

Problem

After parsing a document, and finding some elements, you'll want to get at the data inside those elements.html

Solution

For example:app

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>"

Description

The methods above are the core of the element data access methods. There are additional others:ide

All of these accessor methods have corresponding setter methods to change the data.orm

See also

相關文章
相關標籤/搜索