phantomjs java 爬虫_使用phantomjs进行网页抓取的实现代码

phantomjs因为是无头浏览器可以跑js,所以同样可以跑dom节点,用来进行网页抓取是再好不过了。

比如我们要批量抓取网页 “历史上的今天” 的内容。网站

e3aaaa7dd7c6ff10a5f810bb66354cd4.png

对dom结构的观察发现,我们只需要取到 .list li a的title值即可。因此我们利用高级选择器构建dom片段

var d= ''

var c = document.querySelectorAll('.list li a')

var l = c.length;

for(var i =0;i

d=d+c[i].title+'\n'

}

之后只需要让js代码在phantomjs里跑起来即可~

var page = require('webpage').create();

page.open('http://www.todayonhistory.com/', function (status) { //打开页面

if (status !== 'success') {

console.log('FAIL to load the address');

} else {

console.log(page.evaluate(function () {

var d= ''

var c = document.querySelectorAll('.list li a')

var l = c.length;

for(var i =0;i

d=d+c[i].title+'\n'

}

return d

}))

}

phantom.exit();

});

最终我们另存为catch.js,在dos里面执行一下,输出内容到txt文件(也可以用phantomjs的文件api来写)

a7ce2faa8b734c4dff43b7df99254cc8.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要的添加的jar包及工具:我这里使用maven来构建项目,添加依赖如下:   org.seleniumhq.selenium   selenium-java   3.2.0    PhantomJs工具到官网去下载:http://phantomjs.org/download.html 尽量都使用最新版本,不然会出现版本兼容的情况。 这里有一个已经写好的获取PhantomJSDriver的工具类 public static WebDriver getPhantomJs() {   String osname = System.getProperties().getProperty("os.name");   if (osname.equals("Linux")) {//判断系统的环境win or Linux     System.setProperty("phantomjs.binary.path", "/usr/bin/phantomjs");   } else {     System.setProperty("phantomjs.binary.path", "./phantomjs/win/phantomjs.exe");//设置PhantomJs访问路径   }   DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();   //设置参数   desiredCapabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");   desiredCapabilities.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101   Firefox/50.0");   if (Constant.isProxy) {//是否使用代理     org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();     proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);     proxy.setAutodetect(false);     String proxyStr = "";     do {       proxyStr = ProxyUtil.getProxy();//自定义函数,返回代理ip及端口     } while (proxyStr.length() == 0);     proxy.setHttpProxy(proxyStr);     desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);   }   return new PhantomJSDriver(desiredCapabilities); } 获取方式     try{     WebDriver webDriver = PhantomJsUtil.getPhantomJs();     webDriver.get(url);     SleepUtil.sleep(Constant.SEC_5);     PhantomJsUtil.screenshot(webDriver);     WebDriverWait wait = new WebDriverWait(webDriver, 10);     wait.until(ExpectedConditions.presenceOfElementLocated(By.id(inputId)));//开始打开网页,等待输入元素出现     Document document = Jsoup.parse(webDriver.getPageSource());     //TODO  剩下页面的获取就按照Jsoup获取方式来做   }finally{     if (webDriver != null) {       webDriver.quit();     }   } python版使用webdriver+PhantomJs爬虫使用,参考http://www.cnblogs.com/kuqs/p/6395284.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值