簡體   English   中英

如何使用jsoup發布JavaScript表單?

[英]How to post javascript form using jsoup?

我想從http://www.wettportal.com/quotenarchiv/中提取一些數據。

有一個javascript形式: 搜索

<form id="archivesearchform" name="archivesearchform" method="post" action="">
...
<td class="ralign">Sportart:</td>
<td>
<select name="sport_id" id="sport_id" style="width:100%">
...
<td class="ralign">Land:</td>
<td>
<select name="region_id" id="region_id" style="width:100%;">
...
<td class="ralign">Liga:</td>
<td>
<select name="league_id" id="league_id" style="width:100%">
...
<td class="ralign">vom:</td>
<td>
<input type="text" name="fromdate" id="fromdate" style="width:100%" />
...
<td class="ralign">vom:</td>
<td>
<input type="text" name="fromdate" id="fromdate" style="width:100%" />
</td>
<td class="ralign">bis:</td>
<td>
<input type="text" name="tilldate" id="tilldate" style="width:100%" />
</td>
<td colspan="2"></td>
</tr>
<tr>
<td class="ralign">Teilnehmer:</td>
<td colspan="3"><input type="text" name="team" style="width:100%" /></td>
<td colspan="2"></td>
</tr>
</tbody>

和提交按鈕:

<tr>
<td class="lalign"></td>
<td class="calign"><input type="submit" name="btnSubmit" value="Suchen" /></td>
<td class="ralign"><div class="loading-animation" id="div_loading"></div></td>
</tr>

我嘗試使用以下代碼:

import java.io.IOException; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class QAJesoupE { 

    public static void main(String[] args) { 

        try { 
            Document doc = Jsoup.connect("http://www.wettportal.com/quotenarchiv/")
                .data("sport_id", "4")
                .data("region_id", "16")
                .data("league_id", "0")
                .data("fromdate", "")
                .data("tilldate", "")
                .data("team", "")
                // and other hidden fields which are being passed in post request.
                .userAgent("Mozilla")
                .post();
                System.out.println(doc); // will print html source of homepage of facebook.

        } catch (IOException e) { 
            e.printStackTrace(); 
            } 
    } 
}

但是我只得到HTML代碼,沒有任何搜索結果。 : - /

可以請任何人幫助我嗎?

在此先多謝!

這個站點上有一個腳本來處理表單提交 即使form元素定義了POST ,腳本實際上也會發送帶有數據作為URL參數的get請求:

http://www.wettportal.com/lib/ajax/getArchivedEvents.php?partner=wettportal&lang=de&sport_id=4&region_id=23&league_id=0&fromdate=&tilldate=&team=

Jsoup將為您創建請求URL(帶有參數),但是您將必須發送GET請求並包括X-Requested-With標頭(請參見下文):

Document doc = Jsoup
    .connect("http://www.wettportal.com/lib/ajax/getArchivedEvents.php")
    .data("sport_id", "4")
    .data("region_id", "16")
    .data("league_id", "0")
    .data("fromdate", "")
    .data("tilldate", "")
    .data("team", "")
    .header("X-Requested-With", "XMLHttpRequest")
    .timeout(10000)
    .get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM