美文网首页大数据 爬虫Python AI Sql
JSoup 爬虫遇到的 404 错误解决方案

JSoup 爬虫遇到的 404 错误解决方案

作者: SunY7 | 来源:发表于2023-12-27 16:23 被阅读0次
    亿牛云 (4).png

    在网络爬虫开发中,使用JSoup进行数据抓取是一种常见的方式。然而,当我们尝试使用JSoup来爬虫抓取腾讯新闻网站时,可能会遇到404错误。这种情况可能是由于网站的反面爬虫机制检测到了我们的爬虫行为,从而拒绝了我们的请求。
    假设我们希望使用JSoup来爬取腾讯新闻的数据,但在实际操作中,我们却遇到404错误。这可能是因为腾讯新闻网站采取了一些反爬虫措施,例如检测请求头中的用户- Agent信息或者Referer信息,以识别爬虫行为并拒绝请求并返回404错误信息。如下所示:

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import java.io.IOException;
    
    public class TencentNewsCrawler {
        public static void main(String[] args) {
            String url = "http://news.qq.com/not_existing_page"; // 不存在的页面
    
            try {
                Document document = Jsoup.connect(url).get();
                System.out.println(document.outerHtml());
            } catch (IOException e) {
                System.out.println("Error fetching the page: " + e.getMessage());
                if (e.getMessage().contains("404")) {
                    System.out.println("Encountered 404 error - Page not found");
                }
            }
        }
    }
    
    

    为了解决这个问题,我们可以采取以下几种方法:

    1. 设置合适的请求头:请求头中包含了关于客户端环境和请求的信息,通过设置合适的请求头,我们可以让服务器认为请求来自标准浏览器,从而避免被拒绝或返回404错误。
    2. 模拟浏览器的请求:通过设置合适的User-Agent来模拟浏览器的请求,让服务器认为请求来自标准浏览器,从而避免被拒绝或返回404错误。
    3. 设置Referer信息:有些网站会要求客户端提供特定的Referer信息,即来源页面的URL。通过设置请求头中的Referer字段来模拟请求来源页面的URL,有助于避免被服务器或拒绝返回404错误。
    4. 使用代理服务器:通过使用代理服务器,我们可以隐藏爬虫的真实IP地址,从而降低被网站识别为爬虫的概率。JSoup提供了设置代理的方法,不知道如何设置的可以参考这里https://www.16yun.cn/help/ss_demo/#4java
      通过以上方法,我们可以有效地解决 JSoup 爬虫遇到的 404 错误问题,确保爬虫能够正常地获取所需的数据,完整的实现代码示例如下:
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    import java.io.IOException;
    
    public class JsoupCrawlerWithProxy {
        public static void main(String[] args) {
            String url = "https://example.com"; // 替换为目标网站的URL
            String proxyHost = "www.16yun.cn";
            String proxyPort = "5445";
            String proxyUser = "16QMSOML";
            String proxyPass = "280651";
    
            try {
                // 设置合适的User-Agent和Referer,并使用代理服务器
                Connection connection = Jsoup.connect(url)
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                        .referrer("https://www.google.com")
                        .proxy(proxyHost, Integer.parseInt(proxyPort))
                        .header("Proxy-Authorization", "Basic " + encodeProxyCredentials(proxyUser, proxyPass));
    
                // 发起请求
                Document document = connection.get();
    
                // 处理返回的HTML文档
                System.out.println(document.title());
                // 其他处理逻辑...
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        // 编码代理服务器的用户名和密码
        private static String encodeProxyCredentials(String username, String password) {
            String credentials = username + ":" + password;
            return java.util.Base64.getEncoder().encodeToString(credentials.getBytes());
        }
    }
    
    

    相关文章

      网友评论

        本文标题:JSoup 爬虫遇到的 404 错误解决方案

        本文链接:https://www.haomeiwen.com/subject/hunrndtx.html