gpt4 book ai didi

java - Jsoup 从 HTML 内容中提取 Hrefs

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

我的问题是我尝试使用 JSoup 从此站点获取 Hrefs

https://www.amazon.de/s?k=kissen&__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&ref=nb_sb_noss_2

但是它不起作用。

我尝试像这样从 Href 中选择类

Elements elements = documentMainSite.select(".a-link-normal");

之后我尝试使用以下代码提取 Href。

for (Element element : elements) {
String href = element.attributes().get("href");
}

但不幸的是它没有给我任何东西......

有人可以告诉我我的错误在哪里吗?

<小时/>

我不只是连接到网站。我还通过使用

提取 href 将其保存在字符串中
String href = element.attributes().get("href");

之后我打印了 href 字符串,但它是空的。

另一方面,代码与另一个 css 选择器一起使用。所以它与代码本身无关。只是 css 选择器 (.a-link-normal) 可能是错误的。

最佳答案

仅仅通过 Jsoup 连接到 url 不会得到任何东西。

Document document = Jsoup.connect(yourUrl).get();
String bodyText = document.getElementsByTag("body").get(0).text();

这是我从上面的代码中获得的正文翻译。

Enter the characters below We ask for your understanding and want to be sure that you are not a bot. For best results, please use a browser that accepts cookies. Type the characters you see in the image: Enter characters Try another image Continue shopping Terms & Conditions Privacy Policy © 1996-2015, Amazon.com, Inc. or its affiliates

例如,您需要绕过验证码或通过 Selenium 模拟浏览器。

关于java - Jsoup 从 HTML 内容中提取 Hrefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56907857/

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