java - 从数组中的 jsoup 获取独立标签的文本

标签 java jsoup

我正在使用jsoup来解析html元素

我正在做的是

 Elements e = document.select(".doc-type-list li a");
         System.out.println(e);

这给了我这个

<a class="doc-type doc-type-mtm" href="/mtm/a-d-topical.html">A &amp; D topical</a>
<a class="doc-type doc-type-cdi" href="/cdi/a-d-cracked-skin-relief-cream.html">A + D Cracked Skin Relief cream</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-200-lice-treatment.html">A-200 Lice Treatment</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-25.html">A-25</a>
<a class="doc-type doc-type-cons" href="/cons/a-caro-25.html">A-Caro-25</a>
<a class="doc-type doc-type-cons" href="/cons/a-g-profen.html">A-G Profen</a>
<a class="doc-type doc-type-pro" href="/pro/a-hydrocort.html">A-Hydrocort</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-methapred-injection.html">A-Methapred injection</a>
<a class="doc-type doc-type-cons" href="/cons/a-methapred.html">A-Methapred</a>
<a class="doc-type doc-type-cdi" href="/cdi/a-methapred-solution.html">A-methapred solution</a>
<a class="doc-type doc-type-pro" href="/pro/a-methapred-injection.html">A-Methapred Injection</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-phedrin.html">A-Phedrin</a>
<a class="doc-type doc-type-cdi" href="/cdi/a-spaz.html">A-Spaz</a>
<a class="doc-type doc-type-cdi" href="/cdi/a-tan-12x-suspension.html">A-Tan 12X suspension</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-e-r-witch-hazel.html">A.E.R. Witch Hazel</a>
<a class="doc-type doc-type-cons" href="/cons/a-b-otic.html">A / B Otic</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-fish-oil.html">A / Fish Oil</a>
<a class="doc-type doc-type-mtm" href="/mtm/a-t-s.html">A / T / S</a>
<a class="doc-type doc-type-cons" href="/cons/a-t-s-topical.html">A / T / S Topical</a>
<a class="doc-type doc-type-monograph" href="/monograph/a1-proteinase-inhibitor-human.html">a1-Proteinase Inhibitor (Human)</a>
<a class="doc-type doc-type-cons" href="/cons/a200-maximum-strength-topical.html">A200 Maximum Strength Topical</a>
<a class="doc-type doc-type-cons" href="/cons/a200-time-tested-formula-topical.html">A200 Time-Tested Formula Topical</a>
<a class="doc-type doc-type-mtm" href="/mtm/abacavir.html">abacavir</a>
<a class="doc-type doc-type-cons" href="/cons/abacavir.html">abacavir</a>
<a class="doc-type doc-type-cdi" href="/cdi/abacavir-solution.html">abacavir solution</a>
<a class="doc-type doc-type-cdi" href="/cdi/abacavir.html">abacavir</a>
<a class="doc-type doc-type-ppa" href="/ppa/abacavir.html">Abacavir</a>
<a class="doc-type doc-type-mtm" href="/mtm/abacavir-and-lamivudine.html">abacavir and lamivudine</a>
<a class="doc-type doc-type-cons" href="/cons/abacavir-and-lamivudine.html">abacavir and lamivudine</a>
<a class="doc-type doc-type-ppa" href="/ppa/abacavir-and-lamivudine.html">Abacavir and Lamivudine</a>
<a class="doc-type doc-type-pro" href="/pro/abacavir-and-lamivudine-tablets.html">Abacavir and Lamivudine Tablets</a>
<a class="doc-type doc-type-monograph" href="/monograph/abacavir-sulfate.html">Abacavir Sulfate</a>
<a class="doc-type doc-type-pro" href="/pro/abacavir-sulfate-tablets.html">Abacavir Sulfate Tablets</a>
<a class="doc-type doc-type-mtm" href="/mtm/abacavir-dolutegravir-and-lamivudine.html">abacavir, dolutegravir, and lamivudine</a>
<a class="doc-type doc-type-cons" href="/cons/abacavir-dolutegravir-and-lamivudine.html">abacavir, dolutegravir, and lamivudine</a>
<a class="doc-type doc-type-cdi" href="/cdi/abacavir-dolutegravir-and-lamivudine.html">abacavir, dolutegravir, and lamivudine</a>
<a class="doc-type doc-type-ppa" href="/ppa/abacavir-dolutegravir-and-lamivudine.html">Abacavir, Dolutegravir, and Lamivudine</a>
<a class="doc-type doc-type-pro" href="/pro/abacavir-lamivudine-and-zidovudinetablets.html">Abacavir, Lamivudine and ZidovudineTablets</a>
<a class="doc-type doc-type-mtm" href="/mtm/abacavir-lamivudine-and-zidovudine.html">abacavir, lamivudine, and zidovudine</a>
<a class="doc-type doc-type-cons" href="/cons/abacavir-lamivudine-and-zidovudine.html">abacavir, lamivudine, and zidovudine</a>

但我想要数组中 a 内的文本,即

A & D topical
A + D Cracked Skin Relief cream
A-200 Lice Treatment....
.............

最佳答案

您可以使用一种方法来转换为列表。

public List<String> contents(Elements elements) {
    List<String> list=new ArrayList<>();
    for (Element element : elements) {
        list.add(element.text());
    }
    return list;
}

关于java - 从数组中的 jsoup 获取独立标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590118/

相关文章:

java - 我如何使用 Jsoup 遍历 HTML 树?

java - 实现生产者消费者模式

java - 将 Swing 应用程序转换为 Applet 的复杂性

java - 如何停止重绘()闪烁

java - 将输入字符串转换为二维数组,然后使用索引文件将其提取

java - 使用 HashMap 存储大量数据会降低我的 Android 应用程序的速度,还有其他选择吗?

c# - 在 C# 中将 X509Certificate 与文件和 key 一起使用

java - JSoup,从 HTML 中删除没有标签的文本

java - Jsoup 登录失败 - 系统错误消息

java - 使用 JSoup 调用 JSP 函数