无法使用HtmlUnit和Jsoup组合下载完整文档(使用Java)

时间:2015-06-09 16:07:35

标签: java jsoup htmlunit

问题陈述: 我想抓取此页面:http://www.hongkonghomes.com/en/property/rent/the_peak/middle_gap_road/10305?page_no=1&rec_per_page=12&order=rental+desc&offset=0

让我们说要解析一下这个地址,即“香港山顶中峡道24号”

我做了什么: 我首先只尝试使用jsoup加载,但后来我注意到该页面需要一些时间来加载。所以,然后我也插入HTMLUnit等待页面首先加载

我编写的代码:

public static void parseByHtmlUnit() throws Exception{
        String url = "http://www.hongkonghomes.com/en/property/rent/the_peak/middle_gap_road/10305?page_no=1&rec_per_page=12&order=rental+desc&offset=0";
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
        webClient.waitForBackgroundJavaScriptStartingBefore(30000);
        HtmlPage page = webClient.getPage(url);
        synchronized(page) {
            page.wait(30000);
        }
        try {
            Document doc = Jsoup.parse(page.asXml());
            String address = ElementsUtil.getTextOrEmpty(doc.select(".addr"));
            System.out.println("address"+address);
        } catch (Exception e) {
             e.printStackTrace();
        }
}

预期产量: 在控制台中,我应该得到这个输出: 地址:香港山顶中峡道24号

实际输出: 地址

1 个答案:

答案 0 :(得分:0)

这个怎么样?

final Document document = Jsoup.parse(
    new URL("http://www.hongkonghomes.com/en/property/rent/the_peak/middle_gap_road/10305?page_no=1&rec_per_page=12&order=rental+desc&offset=0"),
    30000
);
System.out.println(document.select(".addr").text());