2

tempobet.com のデータを英語形式で解析しようとしています。問題は、Google レスト クライアントを使用すると、必要に応じて 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"。どんな助けにも感謝します。とにかくありがとう

4

1 に答える 1

5

tempobet がヘッダーを確認するだけなら、とても簡単かもしれませんAccept-Language...

彼らは異なるドメインで tr (tempobet22.com) と en (tempobet.com) を提供しています。en-domain への最初の呼び出しは、tr-domain にリダイレクトされます。別の言語を選択すると、2 つのリダイレクトと魔法のセッション共有が行われます。最初のリダイレクトでは、最初のドメインからの Cookie が必要です。2 番目のリダイレクトGAMBLINGSESSでは、2 番目のドメインの 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();
于 2014-04-17T22:43:24.623 に答える