Home  >  Article  >  Web Front-end  >  [Jsoup Learning Etiquette] Extract attributes, text and HTML_html/css_WEB-ITnose from elements

[Jsoup Learning Etiquette] Extract attributes, text and HTML_html/css_WEB-ITnose from elements

WBOY
WBOYOriginal
2016-06-24 11:48:25827browse

Problem

After parsing to obtain a Document instance object and finding some elements, you want to obtain the data in these elements.

Method

  • To get the value of an attribute, you can use Node.attr(String key) method
  • For text in an element, you can use Element.text() Method
  • To get the HTML content in an element or attribute, you can use Element.html(), or Node.outerHtml() method
  • 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内容

    Description

    The above method is the core method of element data access. In addition, there are other methods that can be used:

  • Element.id()
  • Element.tagName()
  • Element.className() and Element.hasClass(String className)
  • These accessor methods have corresponding setter methods to change data.

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn