【问题标题】:Jsoup set accept-header request doesn't workJsoup 设置接受标头请求不起作用
【发布时间】:2014-06-02 04:07:34
【问题描述】:

我正在尝试解析来自 tempobet.com 的英文格式的数据。问题是当我使用 google rest 客户端时,它会返回与我想要的相同的 html,但是,当我尝试通过 Jsoup 解析它时,它会以我的语言环境格式返回日期格式。这是测试代码

import java.io.IOException;
import java.util.Date;
import java.util.ListIterator;
import java.util.Locale;

import org.apache.commons.lang3.time.DateUtils;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

public class ParseHtmlTest {

    @Test
    public void testName() throws IOException {

        Response response = Jsoup.connect("https://www.tempobet.com/league191_5_0.html")
                                 .userAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36")
                                 .execute();

        Document doc = Jsoup.connect("https://www.tempobet.com/league191_5_0.html")
                            .userAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36")
                            .header("Accept-Language", "en-US")
                            .header("Accept-Encoding", "gzip,deflate,sdch")
                            .cookies(response.cookies())
                            .get();

        Elements tableElement = doc.select("table[class=table-a]");
        ListIterator<Element> trElementIterator = tableElement.select("tr:gt(2)").listIterator();

        while (trElementIterator.hasNext()) {

            ListIterator<Element> tdElementIterator = trElementIterator.next().select("td").listIterator();

            while (tdElementIterator.hasNext()) {

                System.out.println(tdElementIterator.next());
            }
        }
    }
}

这里是响应的示例行

<td width="40" class="grey">21 Nis 20:00</td>

日期应该是"21 Apr 20:00"。我将不胜感激。还是谢谢

【问题讨论】:

    标签: java html-parsing jsoup request-headers


    【解决方案1】:

    如果 tempobet 只看一下Accept-Language Header,那就太容易了...

    他们在不同的域上提供 tr (tempobet22.com) 和 en (tempobet.com)。对 en-domain 的第一次调用被重定向到 tr-domain。如果您选择另一种语言,他们将进行两次重定向和神奇的会话共享。对于第一个重定向,您需要来自第一个域的GAMBLINGSESS cookie,对于第二个域的第二个。 Jsoup 在跟踪重定向时不知道这一点...

    String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
    // get a session for tr and en domain
    String tempobetSession = Jsoup.connect("https://www.tempobet.com/").userAgent(userAgent).execute().cookie("GAMBLINGSESS");
    String tempobet22Session = Jsoup.connect("https://www.tempobet22.com/").userAgent(userAgent).execute().cookie("GAMBLINGSESS");
    // tell tr domain that we wont to go to en without following the redirect
    String redirect = Jsoup.connect("https://www.tempobet22.com/?change_lang=https://www.tempobet.com/")
        .userAgent(userAgent).cookie("GAMBLINGSESS", tempobet22Session).followRedirects(false).execute().header("Location");
    // Redirect goes to en domain including our hashed tr-cookie as parameter - but this redirect needs a en-cookie
    Response response = Jsoup.connect(redirect).userAgent(userAgent).cookie("GAMBLINGSESS", tempobetSession).execute();
    // finally...
    Document doc = Jsoup.connect("https://www.tempobet.com/league191_5_0.html").userAgent(userAgent).cookies(response.cookies()).get();
    

    【讨论】:

    • 哇,真是个魅力!我必须问几个问题,尽管我是 http 的新手 :)。当我检查 cookie 列表时,还有一些 cookie,那么GAMBLINGSESS 是如何足够的呢?其次,当我从 chrome 的高级 REST 客户端发出 GET 请求时,它告诉我有两个重定向。第一个是https://www.tempobet.com/,第二个是https://www.tempobet22.com/。所以这就是我理解有两个重定向的方式吗?。
    • GAMBLINGSESS 是会话cookie,它们似乎在其中存储请求的语言。我不知道他们用last_domain_id cookie 做什么。也许跟踪人们从 tr 切换到 en 到 de 的频率……其他 Cookie 是 GoogleAnalytics 跟踪 Cookie。第一个重定向是?change_lang...(第 4 行)的答案。如果您遵循此重定向,他们会再次向https://www.tempobet.com/ 发送重定向。如果您像普通用户在网站上所做的那样更改语言,您可以在 Chrome DevTools 的 NetworkTab 中轻松看到这一点。
    • 是的,我明白了。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 2016-06-20
    • 1970-01-01
    • 2018-06-24
    • 2014-11-26
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多