Java JSoup错误提取URL(Java JSoup error fetching URL)

编程入门 行业动态 更新时间:2024-10-28 08:23:28
Java JSoup错误提取URL(Java JSoup error fetching URL)

我正在创建一个应用程序,它可以让我从特定网站获取值到控制台。 该值来自<span>元素,我正在使用JSoup

我的挑战与此错误有关:

获取URL时出错

这是我的Java代码:

public class TestSl { public static void main(String[] args) throws IOException { Document doc = Jsoup.connect("https://stackoverflow.com/questions/11970938/java-html-parser-to-extract-specific-data").get(); Elements spans = doc.select("span[class=hidden-text]"); for (Element span: spans) { System.out.println(span.text()); } } }

这是控制台上的错误:

线程“main”中的异常org.jsoup.HttpStatusException:HTTP错误提取URL。 Status = 403,URL = Java Html解析器提取特定数据? at org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:590)org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:540)at org.jsoup.helper.HttpConnection.execute(HttpConnection) .java:227)在testSl.main的org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)(TestSl.java:19)

我做错了什么,如何解决?

I'm creating an application which will enable me to fetch values from a specific website to the console. The value is from a <span> element and I'm using JSoup.

My challenge has to do with this error:

Error fetching URL

Here is my Java code:

public class TestSl { public static void main(String[] args) throws IOException { Document doc = Jsoup.connect("https://stackoverflow.com/questions/11970938/java-html-parser-to-extract-specific-data").get(); Elements spans = doc.select("span[class=hidden-text]"); for (Element span: spans) { System.out.println(span.text()); } } }

And here is the error on Console:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=Java Html parser to extract specific data? at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:590) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216) at TestSl.main(TestSl.java:19)

What am I doing wrong and how can I resolve it?

最满意答案

设置用户代理标头:

.userAgent("Mozilla")

例:

Document document = Jsoup.connect("https://stackoverflow.com/questions/11970938/java-html-parser-to-extract-specific-data").userAgent("Mozilla").get(); Elements elements = document.select("span.hidden-text"); for (Element element : elements) { System.out.println(element.text()); }

堆栈交换

收件箱

声誉和徽章

来源: https : //stackoverflow.com/a/7523425/1048340


也许这是相关的: https : //meta.stackexchange.com/questions/277369/a-terms-of-service-update-restricting-companies-that-scrape-your-profile-informa

Set the user-agent header:

.userAgent("Mozilla")

Example:

Document document = Jsoup.connect("https://stackoverflow.com/questions/11970938/java-html-parser-to-extract-specific-data").userAgent("Mozilla").get(); Elements elements = document.select("span.hidden-text"); for (Element element : elements) { System.out.println(element.text()); }

Stack Exchange

Inbox

Reputation and Badges

source: https://stackoverflow.com/a/7523425/1048340


Perhaps this is related: https://meta.stackexchange.com/questions/277369/a-terms-of-service-update-restricting-companies-that-scrape-your-profile-informa

更多推荐

本文发布于:2023-08-07 01:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   JSoup   Java   fetching   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!