Jsoup设置的accept-header请求不起作用

时间:2014-04-17 18:27:46

标签: java html-parsing jsoup request-headers

我试图用英文格式解析tempobet.com的数据。问题是当我使用谷歌休息客户端它返回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"。我会感激任何帮助。不管怎样,谢谢

1 个答案:

答案 0 :(得分:4)

如果tempobet只是看看Accept-Language标题,那就太容易了......

他们在不同的域上提供tr(tempobet22.com)和en(tempobet.com)。第一次调用en-domain被重定向到tr-domain。如果您选择其他语言,他们会进行两次重定向和魔术会话共享。对于第一个重定向,您需要来自第一个域的GAMBLINGSESS cookie,针对第二个域的第二个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();