您的位置:首页 > Web前端 > HTML

利用selenium获取动态页面的html数据

2013-10-21 15:16 411 查看
selenium调用浏览器获取动态html值,再调用其API,可以很方面获取动态数据。经测试,确实简单易用,至于效率方面就没细究了。代码参考:http://my.oschina.net/flashsword/blog/147334(向原作者致敬)。

【前言】 我看其他文章中说到设置环境变量path,还提及selenium-server和Selenium-rc,我这篇文章没那么复杂,没有设置path,只是使用webdriver技术,调用本机chrome,获取动态页面渲染后的html,在进行解析。代码如下:

public static void main(String[] args) {
System.getProperties().setProperty("webdriver.chrome.driver",
"D:\\Code\\chromedriver.exe"); //这个参数就是【chrome驱动器的位置】
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://www.facejoking.com/top/12001/0");
WebElement webElement = webDriver.findElement(By.id("ColumnContainer"));
List<WebElement> listLink = webElement.findElements(By.tagName("a"));
for (int i = 0; (i + 2) < 50; i += 2) {
System.out.println(listLink.get(i).getAttribute("href") + "/"
+ listLink.get(i + 1).getText());
}
System.out.println("chrome driver");
// 关闭窗口,释放资源。
webDriver.close();
}


【后言】 需要从http://www.seleniumhq.org/download/下载【chrome驱动器】和【java的jar包】。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: