JAVA幻影JS不工作
我正在尝试使用HttpResponse(和HttpClient)、Jsoup和HtmlUnit(第一次尝试成功)获取的html源代码,但没有成功。这三个方法返回给我模糊的html源代码(我用其中的三个方法尝试发送所有的标题) 所以我尝试使用PhantomJS,因为我知道它会等待页面的javascript执行,但也没有成功 有人用过吗 以下是我使用的方法:JAVA幻影JS不工作,java,phantomjs,Java,Phantomjs,我正在尝试使用HttpResponse(和HttpClient)、Jsoup和HtmlUnit(第一次尝试成功)获取的html源代码,但没有成功。这三个方法返回给我模糊的html源代码(我用其中的三个方法尝试发送所有的标题) 所以我尝试使用PhantomJS,因为我知道它会等待页面的javascript执行,但也没有成功 有人用过吗 以下是我使用的方法: public static Document renderPage(String url) { System.setProperty(
public static Document renderPage(String url) {
System.setProperty("phantomjs.binary.path", "/usr/local/share/phantomjs-1.9.8-linux-x86_64/bin/phantomjs");
WebDriver ghostDriver = new PhantomJSDriver();
try {
ghostDriver.manage().timeouts().setScriptTimeout(-1, TimeUnit.DAYS);
ghostDriver.manage().timeouts().pageLoadTimeout(-1, TimeUnit.DAYS);
ghostDriver.get(url);
return Jsoup.parse(ghostDriver.getPageSource());
} finally {
ghostDriver.quit();
}
}
谢谢 这将生成页面的源代码(至少这里有来自HtmlUnit的最新快照)。页面代码仍然包含大量javascript内容,但将其移出应该很容易
try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
final HtmlPage page = webClient.getPage("https://passport.zhaopin.com/org/login");
webClient.waitForBackgroundJavaScript(10000);
System.out.println(page.asXml());
}