使用 JSoup 获取隐藏在 HTML 代码中的 URL

发布于 2024-12-17 21:30:10 字数 563 浏览 2 评论 0原文

我有一段网页(库的东西)的 HTML 代码,例如:

    <div class="qelcontent" id="4ed0e0ba4f1b16.47984984" style="display:block;"> 
<div class="description"><h4 class="first"><b>Amazon.com Product Description</b>
(<a href="https://rads.stackoverflow.com/amzn/click/com/0860783227" rel="nofollow noreferrer">ISBN 0860783227</a>, Hardcover)</h4>

我想从 href 属性获取绝对 URL。我尝试过:

selector = document.select(".first .a[href]");

但它返回了null。我怎样才能得到这个值?

I have a piece of HTML code of a web page (library thing) like:

    <div class="qelcontent" id="4ed0e0ba4f1b16.47984984" style="display:block;"> 
<div class="description"><h4 class="first"><b>Amazon.com Product Description</b>
(<a href="https://rads.stackoverflow.com/amzn/click/com/0860783227" rel="nofollow noreferrer">ISBN 0860783227</a>, Hardcover)</h4>

I want to get the absolute URL from an href attribute. I tried:

selector = document.select(".first .a[href]");

But it returned null. How can I get the value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-12-24 21:30:10

这解决了这个特定问题..不确定它是否适用于您的整个数据集。

    String html = "<div class=\"qelcontent\" id=\"4ed0e0ba4f1b16.47984984\" style=\"display:block;\">" + 
    "<div class=\"description\"><h4 class=\"first\"><b>Amazon.com Product Description</b>" +
    "(<a href=\"http://rads.stackoverflow.com/amzn/click/0860783227\">ISBN 0860783227</a>, Hardcover)</h4>";

    Document doc = Jsoup.parse(html);
    System.out.println(doc.select(".first").select("a").attr("href"));

This solves this specific problem.. not sure if it will work with your entire dataset.

    String html = "<div class=\"qelcontent\" id=\"4ed0e0ba4f1b16.47984984\" style=\"display:block;\">" + 
    "<div class=\"description\"><h4 class=\"first\"><b>Amazon.com Product Description</b>" +
    "(<a href=\"http://rads.stackoverflow.com/amzn/click/0860783227\">ISBN 0860783227</a>, Hardcover)</h4>";

    Document doc = Jsoup.parse(html);
    System.out.println(doc.select(".first").select("a").attr("href"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文