Jsoup爬虫和HTTP错误提取URL

时间:2018-04-02 11:23:41

标签: java http web-crawler jsoup http-status-code-404

我正在使用Jsoup编写一个爬虫程序,这是我得到的HTTP错误:

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/%E2%80%9Chttp:/wildfly.org/downloads/
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:760)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:757)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:706)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:299)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:288)
at testing.DefinitelyNotSpiderLeg.crawl(DefinitelyNotSpiderLeg.java:31)
at testing.DefinitelyNotSpider.search(DefinitelyNotSpider.java:33)
at testing.Test.main(Test.java:9)

我阅读了有关此错误的所有其他类似问题和解决方案,因此我将其解决方案应用到我的代码中,但当Jsoup连接到网址时,我仍然会遇到相同的错误。

这是我用于抓取的方法:

public boolean crawl(String url)
{
    try
    {
         Document htmlDocument = Jsoup.connect(url)
                 .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1")
                 .referrer("http://www.google.com")              
                 .timeout(1000*5) //it's in milliseconds, so this means 5 seconds.              
                 .get();

        Elements linksOnPage = htmlDocument.select("a[href]");

        for(Element link : linksOnPage)
        {    
            String a =link.attr("abs:href");

            if(a.startsWith(url)) {
                this.links.add(a);
            }               
        }            

    }catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (HttpStatusException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;

}

任何想法的人???

2 个答案:

答案 0 :(得分:0)

这是因为网址不正确: -

在您的代码中,您使用的是网址 - https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/%E2%80%9Chttp:/wildfly.org/downloads/

我可以在堆栈跟踪的第一行看到

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/%E2%80%9Chttp:/wildfly.org/downloads/

实际上找不到: - )

答案 1 :(得分:0)

问题不在于代码,问题在于您正在解析的网页中存在的链接。

这是包含更多链接的原始页面。当您抓取网页时,它会为您提供所有链接。 https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/

现在,如果您仔细查看并检查该页面,您将获得一个超链接enter image description here

并且超链接中的代码显示为 - <a href="“http://wildfly.org/downloads/“" target="“_blank”">official website</a>

如果您注意到此url会产生问题,因为它会出现在其中的额外引号,因此它会附加此引号url和基本url togther并且输出为 - https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/%E2%80%9Chttp:/wildfly.org/downloads/
你作为
https://www.mkyong.com/spring-boot/spring-boot-hibernate-search-example/%E2%80%9Chttp:/wildfly.org/downloads/进入JSOUP。因此,要在抓取网页时解决您的问题,您必须执行处理并删除不必要的内容,并将所需的网址http:/wildfly.org/downloads/与搞砸的网址分开或处理代码中的失败。希望它能给你更好的想法。

相关问题