gpt4 book ai didi

java - JSoup 根据 Id 选择 Div,根据 title 选择 href

转载 作者:行者123 更新时间:2023-11-30 11:48:17 24 4
gpt4 key购买 nike

我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。

我的伪代码是这样的

Document divTag = Jsoup.connect(link).get();
Elements info = divTag.select("div#navDiv");

其中 navDiv 是 ID。但它似乎不起作用。

我还想根据一些标题在 Div 中选择 Href,其中 hrefTitle[] 将是字符串数组。因此,在迭代 href 时,我会检查字符串数组中是否存在标题,如果存在,我会将它们添加到列表中,否则忽略。如何在 Div 中选择 href?以及如何选择标题?非常感谢任何意见。

最佳答案

But it doesnt seem to work.

应该 有效。证明:

Document doc = Jsoup.parse("<html><body><div/>" + 
"<div id=\"navDiv\">" +
"<a href=\"href1\">link1</a>" +
"<a href=\"href2\">link2</a><" +
"</div></body></html>");
Element div = doc.select("div#navDiv").first();

现在,我们可以在 div 中选择 a 元素,该元素具有(例如)值为 href 属性href2:

System.out.println(div.select("a[href=href2]"));

输出:

<a href="href2">link2</a>

您可以在这里找到完整的选择器语法:

关于java - JSoup 根据 Id 选择 Div,根据 title 选择 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8912605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com