【问题标题】:How to handle JavaScript redirects in jsoup如何在 jsoup 中处理 JavaScript 重定向
【发布时间】:2017-06-23 09:51:13
【问题描述】:

我有这个网址 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());

【问题讨论】:

  • 在我看来,zara.com/qr/1260020210042 根本没有被重定向。它返回 200 OK。
  • 是的,但如果你点击链接,它会
  • 那么很可能是js相关的。用 HtmlUnit 试试,然后用 jsoup 使用重定向的 url。
  • @FredericKlein 感谢您的回答,我尝试了那里的代码,它正在向我抛出 net.sourceforge.htmlunit.corejs.javascript.EvaluatorException: JAvascriptvalue is a type com.gargoylesoftware.htmlunit.ScriptException跨度>

标签: java cookies jsoup htmlunit


【解决方案1】:

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 设置为 USA + English。然后就可以了。

享受吧!

【讨论】:

  • 问题是真实浏览器的行为方式相同。尝试使用已删除 cookie 和缓存的浏览器:打开 URL 时,您首先必须选择一个国家/地区,然后单击“确定”。然后你会被重定向到一个错误页面,这是 Zara 主页本身的问题。只有这样,如果您下次打开相同的 URL,它就会起作用。像 HtmlUnit 这样总是以新会话开始的浏览器没有这些 cookie,因此它根本无法工作,因为您模拟了一个新用户。 HtmlUnit 的行为就像普通浏览器一样,对于新用户来说,自己试试吧!
  • 好的,我已经更新了答案,现在可以了。可以看到需要某个cookie,以及如何在HtmlUnit中设置。
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 2021-09-13
  • 2020-03-16
相关资源
最近更新 更多