gpt4 book ai didi

java - 如何解析 HTML 标题

转载 作者:行者123 更新时间:2023-12-02 08:10:29 25 4
gpt4 key购买 nike

我正在解析这个 HTML。

<div id="articleHeader">
<h1 class="headline">Assassin's Creed Revelations: The Three Heroes</h1>
<h2 class="subheadline">Exclusive videos and art spanning three eras of assassins.</h2>
<h2 class="publish-date"><script>showUSloc=(checkLocale('uk')||checkLocale('au'));document.writeln(showUSloc ? '<strong>US, </strong>' : '');</script>

<span class="us_details">September 22, 2011</span>

我想要做的是解析“标题”子标题并将日期全部发布到单独的字符串

最佳答案

只需使用正确的 CSS selectors捕获他们。

Document document = Jsoup.connect(url).get();
String headline = document.select("#articleHeader .headline").text();
String subheadline = document.select("#articleHeader .subheadline").text();
String us_details = document.select("#articleHeader .us_details").text();
// ...

或者更高效一点:

Document document = Jsoup.connect(url).get();
Element articleHeader = document.select("#articleHeader").first();
String headline = articleHeader.select(".headline").text();
String subheadline = articleHeader.select(".subheadline").text();
String us_details = articleHeader.select(".us_details").text();
// ...

关于java - 如何解析 HTML 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7523738/

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