让我们说要解析一下这个地址,即“香港山顶中峡道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号
实际输出: 地址
答案 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());