【问题标题】:Jsoup, parse html loaded ajax tableJsoup,解析html加载的ajax表
【发布时间】:2018-02-05 09:30:28
【问题描述】:

表格解析有问题,加载了ajax:

Document doc = Jsoup.connect("http://lfl.ru/club553").get();

这就是我得到的:

<div class="tournament_stats_table_tournament_3442 tournament-stats-table" style="display: block;" url="/?ajax=1&amp;method=tournament_stats_table&amp;tournament_id=3442&amp;club_id=553">
                        подождите...                    </div>

请告诉我在这种情况下可以做些什么。 谢谢。

【问题讨论】:

标签: java ajax web-scraping jsoup html-parsing


【解决方案1】:

首先:您需要获取 Cookie,只需使用

 Connection.Response = Jsoup.connect.method(get).excute 

然后:使用 cookie 和标头发出请求

document=Jsoup.connect.header.data.post

例如

Connection.Response loginForm=Jsoup.connect("http://www.a5.cn").
        method(Connection.Method.GET).execute();

Document document=Jsoup.connect("http://www.a5.cn/login.html").
        data("formhash","97bfbf").data("hdn_refer","http://www.a5.cn/")
        data("account","userID").data("autoLogin","1").data("password","your password").
        cookies(loginForm.cookies()).header("Accept","application/json, text/javascript, */*; q=0.01").header("X-Requested-With","XMLHttpRequest").post();

System.out.println(document.body().text());

【讨论】:

    【解决方案2】:

    您将无法使用 server->server 请求获取数据。当您在服务器上执行请求时,JS 将不会被执行,因此表根本不可用。

    另外想想这些:

    1. 如果您是所解析网站的所有者 - 尽可能避免使用 ajax;
    2. 检查ajax请求的端点是什么,只需解析它而不是网页;

    【讨论】:

    • 感谢您的回答。我不是网站所有者,我需要在我的网站上自动更新此表。
    • @ivanVishnyak 检查 ajax 请求并解析它的结果。
    猜你喜欢
    • 2015-08-17
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2019-10-10
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多