爬虫爬取数据遇到302,301重定向如何获取重定向后的地址(完美解决)

当用java或者python爬取目标网站的时候,浏览器可以正确重定向,而用编程爬取始终是code:200

只需要将请求头修改成如下,可以根据需要进行更改

  Map<String, String> headers = Map.of(
                    "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                    "Accept-Encoding", "gzip, deflate, sdch, br",
                    "Accept-Language", "zh-CN,zh;q=0.8",
                    "Connection", "keep-alive",
                    "Host", "www.baidu.com",
                    "Upgrade-Insecure-Requests", "1",
                    "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
            );

然后就可以获取目标重定向后的地址

String redirectedUrl = connection.getHeaderField("Location");

完整java语言get请求获取重定向地址方法

   /**
     * 获取重定向后的地址
     * @param url
     * @return
     */
 public static String sendGetRequestWithRedirect(String url) {
        try {
            URL getUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
            connection.setRequestMethod("GET");

            // 设置请求头,模拟浏览器行为
            // 设置自定义请求头
            Map<String, String> headers = Map.of(
                    "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                    "Accept-Encoding", "gzip, deflate, sdch, br",
                    "Accept-Language", "zh-CN,zh;q=0.8",
                    "Connection", "keep-alive",
                    "Upgrade-Insecure-Requests", "1",
                    "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
            );

            // 添加自定义请求头
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                connection.setRequestProperty(entry.getKey(), entry.getValue());
            }
            // 设置重定向处理
            connection.setInstanceFollowRedirects(false);

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
                String redirectedUrl = connection.getHeaderField("Location");

                if (redirectedUrl != null) {
                    // 重定向时获取新地址
                    return redirectedUrl;
                } else {
                    return  url;
                }
            } else {
                // 处理错误响应
                System.out.println("Error response code: " + responseCode);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Java中,可以使用HttpURLConnection类来获取302重定向地址。 首先,我们需要创建一个URL对象,传入目标网址作为参数。然后,使用openConnection()方法打开URL连接,并将其转换为HttpURLConnection对象。 接下来,我们可以通过调用getFollowRedirects()方法来检查自动重定向是否启用。如果启用了自动重定向,则我们可以直接调用getResponseCode()方法来获取响应码。如果响应码是302,则可以通过调用getHeaderField()方法来获取重定向地址。 如果自动重定向没有启用,我们可以通过设置setInstanceFollowRedirects()方法来启用重定向,然后再调用getResponseCode()方法来获取响应码。如果响应码是302,则可以通过调用getHeaderField()方法来获取重定向地址。 以下是一个示例代码: ```java import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class GetRedirectURL { public static void main(String[] args) { try { URL url = new URL("目标网址"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 检查自动重定向是否启用 boolean followRedirects = HttpURLConnection.getFollowRedirects(); // 如果自动重定向启用 if (followRedirects) { int responseCode = connection.getResponseCode(); // 如果响应码是302 if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { String redirectURL = connection.getHeaderField("Location"); System.out.println("重定向地址: " + redirectURL); } } // 如果自动重定向没有启用 else { connection.setInstanceFollowRedirects(true); int responseCode = connection.getResponseCode(); // 如果响应码是302 if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { String redirectURL = connection.getHeaderField("Location"); System.out.println("重定向地址: " + redirectURL); } } } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中,将“目标网址”替换为实际需要获取重定向地址的网址。运行代码后,将会输出重定向地址

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不一样的老墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值