gpt4 book ai didi

java - 如何在 jsoup 中处理 JavaScript 重定向

转载 作者:行者123 更新时间:2023-12-01 08:55:18 25 4
gpt4 key购买 nike

我有这个网址http://www.zara.com/qr/1260020210042,我正在尝试获取重定向的最终网址:

    String url = "http://www.zara.com/qr/1260020210042";
Response response = Jsoup.connect(url).followRedirects(true).execute();
String url2 = response.url().toString();
Response response2 = Jsoup.connect(url2).followRedirects(true).execute();
System.out.println(response2.url());

但它不打印最终重定向的 URl ,我应该更改什么?谢谢,

编辑:

我也尝试过使用 Htmlunit,但它没有给我我需要的最终链接:

        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(true);
HtmlPage page = (HtmlPage) webClient.getPage("http://www.zara.com/qr/1260020210042");
WebResponse response = page.getWebResponse();
String content = response.getContentAsString();
System.out.println(page.getUrl());

最佳答案

Frederic Klein 建议的 HtmlUnit 解决方案实际上效果很好,但有一个与 cookie 相关的警告,请参阅下面的“更新”评论。

首先将此依赖项添加到您的 Maven 配置中:

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.25</version>
</dependency>

然后像这样使用它:

package de.scrum_master.stackoverflow;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static com.gargoylesoftware.htmlunit.BrowserVersion.CHROME;
import static java.util.logging.Level.OFF;
import static java.util.logging.Logger.getLogger;

public class Application {
public static void main(String[] args) throws IOException {
WebClient webClient = createWebClient();
String originalURL = "http://www.zara.com/qr/1260020210042";
String redirectedURL = webClient.getPage(originalURL).getUrl().toString();
Response response = Jsoup.connect(redirectedURL).execute();
System.out.println(response.url());
}

private static WebClient createWebClient() throws MalformedURLException {
getLogger("com.gargoylesoftware").setLevel(OFF);
WebClient webClient = new WebClient(CHROME);
WebClientOptions options = webClient.getOptions();
options.setJavaScriptEnabled(true);
options.setRedirectEnabled(true);
// IMPORTANT: Without the country/language selection cookie the redirection does not work!
webClient.addCookie("storepath=us/en", new URL("http://www.zara.com/"), null);
return webClient;
}
}

控制台日志显示:

http://www.zara.com/us/en/man/shoes/leather/brown-braided-leather-ankle-boots-c0p4065286.html
<小时/>

更新:好的,我找到了您问题的根本原因。这不是 HtmlUnit,而是事实:在用户首次访问任何浏览器期间手动选择国家/地区 + 语言之前,zara.com 上的重定向不起作用。该信息存储在名为 storefront 的 cookie 中,没有该 cookie,每个浏览器 session 将始终再次登陆首页并显示国家/地区选择对话框。我已经更新了我的示例代码,以便将该 cookie 设置为美国 + 英语。然后就可以了。

享受吧!

关于java - 如何在 jsoup 中处理 JavaScript 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com