【问题标题】:How to parse a hidden Javascript section from a HTML page如何从 HTML 页面解析隐藏的 Javascript 部分
【发布时间】:2016-12-20 18:08:16
【问题描述】:

我想从这个 URL 上的隐藏日历中解析 9 月的日期+价格:http://www.lufthansa.com/vol/vol-paris-berlin。这里的问题是,当您按 9 月时,页面将生成日历,但 url 没有变化。我使用了这段代码,但没有结果。

public static void main(String[] args) 
        throws FailingHttpStatusCodeException, MalformedURLException, IOException {

    WebClient webClient = new WebClient();
    HtmlPage myPage = webClient.getPage("http://www.lufthansa.com/vol/vol-paris-berlin");
    Document doc = Jsoup.parse(myPage.asXml());

    for(Element s : doc.select("button.daygrid_cell.hasprice")) {
        String weekday_text = s.select(".weekday_text").text();
        String pricebox = s.select(".pricebox > .br").text();
        System.out.println(
                String.format(
                        "weekday_text=%s pricebox=%s", 
                        weekday_text, 
                        pricebox));
    }

    webClient.close();}

【问题讨论】:

    标签: javascript java jsoup htmlunit


    【解决方案1】:

    我自己目前看不到使用 htmlUnit 的方法。

    不过,您可以“去掉中间人”,使用 lufthansa 页面用来填充日历视图的相同查询:

    https://bestprice-live-backend.mcon.net/flights-by-day?l=fr_fr&departure=PAR&destination=BER&departureFrom=2016-09-01&departureTo=2016-09-30&cabin=Economy&duration=7

    响应采用 JSON 格式,因此您可以使用 JSON 解析器在与 lufthansa 页面上相同的演示文稿中提取信息(价格总是四舍五入到下一个整数)。在以下示例中,我使用了json-simple

    Map<String, Integer> prices = new TreeMap<String, Integer>(); // sorted map/keys in sorted order
    
    try {
        Document doc = Jsoup
                    .connect("https://bestprice-live-backend.mcon.net/flights-by-day?l=fr_fr&departure=PAR&destination=BER&departureFrom=2016-09-01&departureTo=2016-09-30&cabin=Economy&duration=7")
                    .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36")
                    .referrer("http://www.lufthansa.com/vol/vol-paris-berlin")
                    .get();
    
        JSONObject obj = (JSONObject) new JSONParser().parse(doc.text());
    
        obj = (JSONObject) obj.get("dates");
    
        for (Iterator<?> iterator = obj.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            JSONObject dateObject = (JSONObject) obj.get(key);
            Double price = (Double) dateObject.get("price");
            int roundedPrice = (int) Math.ceil(price); // lufthansa displays prices rounded up
            prices.put(key, roundedPrice);
        }
    
        for (String key : prices.keySet()) {
            System.out.println(key + ": " + prices.get(key) + " €");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    输出:

    2016-09-01: 163 €
    2016-09-02: 158 €
    2016-09-03: 160 €
    2016-09-04: 160 €
    2016-09-05: 160 €
    2016-09-06: 158 €
    2016-09-07: 155 €
    2016-09-08: 159 €
    2016-09-09: 160 €
    2016-09-10: 156 €
    2016-09-11: 160 €
    2016-09-12: 159 €
    2016-09-13: 157 €
    2016-09-14: 158 €
    2016-09-15: 160 €
    2016-09-16: 184 €
    2016-09-17: 156 €
    2016-09-18: 160 €
    2016-09-19: 179 €
    2016-09-20: 159 €
    2016-09-21: 163 €
    2016-09-22: 180 €
    2016-09-23: 188 €
    2016-09-24: 160 €
    2016-09-25: 160 €
    2016-09-26: 160 €
    2016-09-27: 154 €
    2016-09-28: 157 €
    2016-09-29: 159 €
    2016-09-30: 163 €
    

    【讨论】:

    • 我理解你的方法,它看起来很合乎逻辑,但我找不到你提取此链接的方式[链接]bestprice-live-backend.mcon.net/…
    • 使用 chrome 开发者工具 (Ctrl+Shift+I) 查看 Network 选项卡,当您单击 Septembre 的按钮时,您可以看到请求和响应。
    • 完美运行!!
    • 回答了新问题。也可以再次查看这里的答案(添加了引用者和用户代理,以稳定请求)。
    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 2012-11-19
    • 2020-05-06
    • 2012-11-25
    • 2018-12-03
    • 2010-12-10
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多