多应用+插件架构,代码干净,支持一键云编译,码云点赞13K star,4.8-4.12 预售价格198元 广告
# 从元素抽取属性,文本和HTML <div><h2>问题</h2> <p>在解析获得一个Document实例对象,并查找到一些元素之后,你希望取得在这些元素中的数据。</p> <h2>方法</h2> <ul> <li>要取得一个属性的值,可以使用<code><a title="Get an attribute's value by its key." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#attr%28java.lang.String%29">Node.attr(String key)</a></code> 方法 </li> <li>对于一个元素中的文本,可以使用<code><a title="Gets the combined text of this element and all its children." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#text%28%29">Element.text()</a></code>方法</li> <li>对于要取得元素或属性中的HTML内容,可以使用<code><a title="Retrieves the element's inner HTML." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#html%28%29">Element.html()</a></code>, 或 <code><a title="Get the outer HTML of this node." href="http://jsoup.org/apidocs/org/jsoup/nodes/Node.html#outerHtml%28%29">Node.outerHtml()</a></code>方法</li> </ul> <p>示例:</p> <pre><code>String html = "&lt;p&gt;An &lt;a href='http://example.com/'&gt;&lt;b&gt;example&lt;/b&gt;&lt;/a&gt; link.&lt;/p&gt;"; 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(); // "&lt;a href="http://example.com"&gt;&lt;b&gt;example&lt;/b&gt;&lt;/a&gt;" String linkInnerH = link.html(); // "&lt;b&gt;example&lt;/b&gt;"//取得链接内的html内容 </code></pre> <h2>说明</h2> <p>上述方法是元素数据访问的核心办法。此外还其它一些方法可以使用:</p> <ul> <li><code><a title="Get the id attribute of this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#id%28%29">Element.id()</a></code></li> <li><code><a title="Get the name of the tag for this element." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#tagName%28%29">Element.tagName()</a></code></li> <li><code><a title="Gets the literal value of this element's &quot;class&quot; attribute, which may include multiple class names, space separated." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#className%28%29">Element.className()</a></code> and <code><a title="Tests if this element has a class." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#hasClass%28java.lang.String%29">Element.hasClass(String className)</a></code></li></ul> <p>这些访问器方法都有相应的setter方法来更改数据.</p> <h2>参见</h2> <ul> <li><code><a title="A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements)." href="http://jsoup.org/apidocs/org/jsoup/nodes/Element.html">Element</a></code>和<code><a title="A list of Elements, with methods that act on every element in the list." href="http://jsoup.org/apidocs/org/jsoup/select/Elements.html">Elements</a></code>集合类的参考文档</li> <li><a href="http://www.open-open.com/jsoup/working-with-urls.htm">URLs处理</a></li> <li><a href="http://www.open-open.com/jsoup/selector-syntax.htm">使用CSS选择器语法来查找元素</a></li></ul></div>