简体   繁体   English

如何使用Jsoup获取此文本?

[英]How do I get this text using Jsoup?

How do i get "this text" from the following html code using Jsoup? 如何使用Jsoup从以下html代码中获取“此文本”?

<h2 class="link title"><a href="myhref.html">this text<img width=10 
        height=10 src="img.jpg" /><span class="blah">
        <span>Other texts</span><span class="sometime">00:00</span></span>
        </a></h2>

When I try 当我尝试

String s = document.select("h2.title").select("a[href]").first().text();

it returns 它返回

this textOther texts00:00 本文其他文本00:00

I tried to read the api for Selector in Jsoup but could not figure out much. 我试着在Jsoup中读取Selector的api但是想不通多了。

Also how do i get an element of class class="link title blah" (multiple classes?). 另外我如何获得class class="link title blah"的元素(多个类?)。 Forgive me I only know both Jsoup and CSS a little. 原谅我,我只知道Jsoup和CSS。

Use Element#ownText() instead of Element#text() . 使用Element#ownText()而不是Element#text()

String s = document.select("h2.link.title a[href]").first().ownText();

Note that you can select elements with multiple classes by just concatenating the classname selectors together like as h2.link.title which will select <h2> elements which have at least both the link and title class. 请注意,您可以通过将类名选择器连接在一起来选择具有多个类的元素,如h2.link.title ,它将选择至少同时具有linktitle类的<h2>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM