尝试在Jsoup中加载URL的重定向过多

时间:2018-10-23 11:45:29

标签: java jsoup

我正在尝试抓取一些mediafire链接,并遇到了在Internet上进行过彻底搜索的重定向过多的问题,但是我面临的问题是,某些URL不会在出现“重定向过多”错误的同时其中一些确实可以,因为我做错了,请您帮我吗?

try{
        String url = "http://www.mediafire.com/file/110n342iorl685e/Guns-n%27-Roses_Paradise-City-TS_v1_2_DD_p.psarc";
        Document doc3 = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(0).get();
            for (Element sub3childrow : doc3.select("div.download_link")) {
                String link=sub3childrow.select("a").attr("href");
                System.out.println(link);
            }
    }catch(Exception ex){
            ex.printStackTrace();
        }

这是堆栈跟踪:

java.io.IOException: Too many redirects occurred trying to load URL http://www.mediafire.com/file/110n342iorl685e/Guns-n%2527-Roses_Paradise-City-TS_v1_2_DD_p.psarc
at org.jsoup.helper.HttpConnection$Response.<init>(HttpConnection.java:623)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:656)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249)
at custom_file_downloader.RedirectExample.main(RedirectExample.java:23)

我试图设置followRedirects(true),反之亦然,但这无济于事。您能在这方面指导我吗?

1 个答案:

答案 0 :(得分:0)

org.jsoup jar的版本 1.10.2 存在问题。引用错误修复提交消息。

  
      
  • 错误修正:在Jsoup.Connection中,如果重定向包含带有%xx转义的查询字符串,则它们将被两次转义。   在遵循重定向之前,导致获取错误的位置。
  •   

详细阅读本https://github.com/jhy/jsoup/issues/826。以上问题在 1.10.3 版本中得到了解决。因此,只需更新版本,然后重试。

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.10.3</version>
    </dependency>

您将获得具有上述依赖性的输出。