Home >Web Front-end >HTML Tutorial >[Jsoup Learning Etiquette] Extract attributes, text and HTML_html/css_WEB-ITnose from elements
After parsing to obtain a Document instance object and finding some elements, you want to obtain the data in these elements.
Example:
String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";Document doc = Jsoup.parse(html);//解析HTML字符串返回一个Document实现Element link = doc.select("a").first();//查找第一个a元素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>"//取得链接内的html内容
The above method is the core method of element data access. In addition, there are other methods that can be used:
These accessor methods have corresponding setter methods to change data.