如何使用Jsoup获取此文本? [英] How do I get this text using Jsoup?

查看:554
本文介绍了如何使用Jsoup获取此文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Jsoup从以下html代码中获取此文本?

 < h2 class = title>< a href =myhref.html>此文字< img width = 10 
height = 10 src =img.jpg/>< span class =blah>
< span>其他文本< / span>< span class =sometime> 00:00< / span>< / span&
< / a>< / h2>

当我尝试

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

它返回


this text其他文字00:00


我尝试阅读选择器,但无法弄清楚。



我得到类 class =link title blah(多个类?)的元素。原谅我我只知道Jsoup和CSS一点。

解决方案

使用 Element#ownText() ,而不是 Element#text()

  String s = document.select(h2.link.title a [href])。first()。ownText请注意,您可以通过将类名选择器连接在一起来选择具有多个类的元素,如<$ c <$ c <$> 

$ c> h2.link.title
,其将选择< h2> 元素,其至少 code>链接
title 类。


How do i get "this text" from the following html code using Jsoup?

<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

I tried to read the api for Selector in Jsoup but could not figure out much.

Also how do i get an element of class class="link title blah" (multiple classes?). Forgive me I only know both Jsoup and CSS a little.

解决方案

Use Element#ownText() instead of 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.

这篇关于如何使用Jsoup获取此文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆