Selenium+PhantomJs 爬取网页内容
利用Selenium和PhantomJs 可以模拟用户操作,爬取大多数的网站。下面以新浪财经为例,我们抓取新浪财经的新闻版块内容。

1.依赖的jar包。我的项目是普通的SSM单间的WEB工程。最后一个jar包是用来在抓取到网页dom后做网页内容解析的。
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.2.0</version>
</dependency> <dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency> <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency> <dependency>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>2.2</version>
</dependency>
2.获取网页dom内容
package com.nsjr.grab.util; import java.util.List;
import java.util.concurrent.TimeUnit; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities; import cn.wanghaomiao.xpath.model.JXDocument; @SuppressWarnings("deprecation")
public class SeleniumUtil { public static JXDocument getDocument(String driverUrl,String pageUrl){
JXDocument jxDocument = null;
PhantomJSDriver driver = null;
try{
System.setProperty("phantomjs.binary.path", driverUrl);
System.setProperty("webdriver.chrome.driver", driverUrl); DesiredCapabilities dcaps = new DesiredCapabilities();
//ssl证书支持
dcaps.setCapability("acceptSslCerts", true);
//截屏支持
dcaps.setCapability("takesScreenshot", true);
//css搜索支持
dcaps.setCapability("cssSelectorsEnabled", true);
//js支持
dcaps.setJavascriptEnabled(true);
//驱动支持
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,driverUrl);
//创建无界面浏览器对象
driver = new PhantomJSDriver(dcaps);
//WebDriver driver = new ChromeDriver(dcaps);
driver.get(pageUrl);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Document document = Jsoup.parse(driver.getPageSource());
jxDocument = new JXDocument(document);
}catch(Exception e){
e.printStackTrace();
}finally{
if(driver != null){
driver.quit();
}
}
return jxDocument;
} public static String getProperty(List<Object> list){
if(list.isEmpty()){
return "";
}else{
return list.get(0).toString();
}
}
}
3.解析并保存内容
JXDocument jxDocument = SeleniumUtil.getDocument(captureUrl.getDriverUrl(), captureUrl.getSinaNews());
//保存第一部分加粗新闻
List<Object> listh3 = jxDocument.sel("//div[@id='impNews1']/div[@id='fin_tabs0_c0']/div[@id='blk_hdline_01']/h3/a");
for(Object a :listh3){
JXDocument doc = new JXDocument(a.toString());
//System.out.println("地址:"+doc.sel("//a/@href"));
//System.out.println("标题:"+doc.sel("//text()"));
saveNews(SeleniumUtil.getProperty(doc.sel("//text()")), SeleniumUtil.getProperty(doc.sel("//a/@href")), Constant.NEWS_TYPE_BOTTOM, Constant.NEWS_SOURCE_SINA);
}
//保存其余新闻
List<Object> listP = jxDocument.sel("//div[@id='impNews1']/div[@id='fin_tabs0_c0']/div[@id='blk_hdline_01']/p/a");
for(Object a :listP){
JXDocument doc = new JXDocument(a.toString());
//System.out.println("地址:"+doc.sel("//a/@href"));
//System.out.println("标题:"+doc.sel("//text()"));
saveNews(SeleniumUtil.getProperty(doc.sel("//text()")), SeleniumUtil.getProperty(doc.sel("//a/@href")), Constant.NEWS_TYPE_BOTTOM, Constant.NEWS_SOURCE_SINA);
}
//保存第二部分新闻
List<Object> listpart2 = jxDocument.sel("//div[@id='impNews1']/div[@id='fin_tabs0_c0']/div[2]/ul");
for(Object a :listpart2){
JXDocument doc = new JXDocument(a.toString());
List<Object> alist = doc.sel("//li/a");
for(Object a2 :alist){
JXDocument doc2 = new JXDocument(a2.toString());
//System.out.println("地址:"+doc2.sel("//a/@href"));
//System.out.println("标题:"+doc2.sel("//text()"));
saveNews(
SeleniumUtil.getProperty(doc2.sel("//text()")),
SeleniumUtil.getProperty(doc2.sel("//a/@href")),
Constant.NEWS_TYPE_BOTTOM,
Constant.NEWS_SOURCE_SINA
);
}
}
4.解释
captureUrl.getDriverUrl(), captureUrl.getSinaNews() 这两个地址分别是PhantomJs工具的地址和要爬取的网站的地址,其中
sina_news = https://finance.sina.com.cn/
driverUrl= D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe
关于PhantomJs 工具的下载可以直接去官网下载 http://phantomjs.org/download.html,有windows 和Linux版供下载。关于网页结构的解析使用JsoupXpath ,是一个国人写的html文档解析工具包,挺好用的。语法可以参考Xpath的相关语法进行节点的选取。
5.爬取结果。由于项目需求较为简单,对实时性和性能要求不高,所以只做到入库,即可满足需求。

最后,刚开始接触爬虫类的东西,有的需求webmagic 可以满足,有的需要其他方式,需要具体问题具体分析。尚在摸索阶段,本文仅仅是提供一种解决思路。
Selenium+PhantomJs 爬取网页内容的更多相关文章
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
Python3.x:Selenium+PhantomJS爬取带Ajax.Js的网页 前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但 ...
- selenium+phantomjs爬取京东商品信息
selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...
- selenium+phantomjs爬取bilibili
selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...
- python+selenium+PhantomJS爬取网页动态加载内容
一般我们使用python的第三方库requests及框架scrapy来爬取网上的资源,但是设计javascript渲染的页面却不能抓取,此时,我们使用web自动化测试化工具Selenium+无界面浏览 ...
- selenium + PhantomJS 爬取js页面
from selenium import webdriver import time _url="http://xxxxxxxx.com" driver = webdriver.P ...
- selenium + phantomjs 爬取落网音乐
题记: 作为一个业余程序猿,最大的爱好就是电影和音乐了,听音乐当然要来点有档次的.落网的音乐的逼格有点高,一听听了10年.学习python一久了,于是想用python技术把落网的音乐爬下来随便听. 目 ...
- Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页及获取JS返回值
前言 现在很多网站的都大量使用JavaScript,或者使用了Ajax技术.这样在网页加载完成后,url虽然不改变但是网页的DOM元素内容却可以动态的变化.如果处理这种网页是还用requests库或者 ...
- 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
这里只是学习一下动态加载页面内容的抓取,并不适用于所有的页面. 使用到的工具就是python selenium和phantomjs,另外调试的时候还用了firefox的geckodriver.exe. ...
- selenium+phantomjs爬取动态页面数据
1.安装selenium pip/pip3 install selenium 注意依赖关系 2.phantomjs for windows 下载地址:http://phantomjs.org/down ...
随机推荐
- 吴恩达+neural-networks-deep-learning+第二周作业
Logistic Regression with a Neural Network mindset v4 简单用logistic实现了猫的识别,logistic可以被看做一个简单的神经网络结构,下面是 ...
- C# 常用方法——图片转base64字符串
其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html /// <summary> /// Image 转成 base64 / ...
- postman如何绕过登录账户和密码验证,进行接口测试的方法
实测于:2019.01.08 参考原文:https://yq.aliyun.com/ziliao/403942 一.获取登录后的cookie数据 1.打开浏览器: 2.启用开发者模式(F12键): 3 ...
- mysql 将时间戳与日期时间的转换
from_unixtime()是MySQL里的时间函数 mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' ) ->20071120 mys ...
- golang string、int、int64 float 互相转换
#string到int int,err := strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, ...
- 记一次elastic-job使用
当当的elastic-job定时任务 业务场景是定时从微信取accesstoken和jsticket,因为都只有7200秒的有效时间,所以设置了定时任务,定时将得到的数据存到redis缓存中 问题1: ...
- 文件读取及比较&文件信息保存
#include <stdio.h> #include <stdlib.h> //#include <regex.h> char* file_name_1 = &q ...
- cucumber+selenium
工程结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- shell 并发进程的例子
linux shell 实现后台多进程运行的,开始和终止 原创 2014年11月21日 12:04:51 9953 linux shell 实现后台开始,和停止多进程 知识点: xxxxcmd.sh ...
- centos7.7下docker与k8s安装(DevOps三)
1.系统配置 centos7.7 docker 1.13.1 centos7下安装docker:https://www.cnblogs.com/pu20065226/p/10536744.html 2 ...