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

WBOY
Release: 2016-06-24 11:48:25
Original
827 people have browsed it

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内容
    Copy after login

    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.

    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template