【问题标题】:Using Jsoup CSS selector for the text between 2 elements对 2 个元素之间的文本使用 Jsoup CSS 选择器
【发布时间】:2014-07-07 00:04:21
【问题描述】:

全部。我正在使用 jsoup css 选择器进行网页抓取,但不知道如何获取 2 个标签之间的文本。如下图:

<html>
<body>

<a name = "xxx" > 这里有一些文字</a>

<a name = "abc"> 文字 1 </a>`

我想在这里获取文本

<aname = "cde">文本 1 </a>

</body>
</html>

&lt;a&gt;的属性名在hmtl上是唯一的

我发现大多数答案是提取&lt;a&gt;&lt;/a&gt;之间的文本

任何人都知道如何做到这一点。非常感谢

【问题讨论】:

    标签: html css jsoup


    【解决方案1】:

    好的,我编写了这个对我有用的小代码

    String html = "<html> <body><a name = xxx > some text here</a><a name = abc> the text 1 </a>i wanna get the text here<a name = cde>the text 1 </a></body></html>";
        Document doc = Jsoup.parse(html);
        String body = doc.body().toString();
        String[] a = body.split("</a>");
        String lista = a[2];
        String[] listb = lista.split("<");      
        String textBetweenA = listb[0];
        System.out.println(textBetweenA);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 2013-11-25
    • 2014-12-04
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多