日前有采集需求,当我把所有的对应页面的链接都拿到手,准备开始根据链接去采集(写爬虫爬取)对应的终端页的时候,发觉用程序获取到的数据根本没有对应 的内容,可是我的浏览器看到的内容明明是有的,于是浏览器查看源代码也发觉没有,此时想起该网页应该是ajax加载的。不知道ajax的小朋友可以去学下 web开发啦。
采集ajax生成的内容手段不外乎两种。一种是通过http观察加载页面时候的请求,然后我们模仿该请求去得到对应的内容,第二种则是模仿浏览器行为去渲 染这个页面得到内容。我在这里决定采用第二种方式,之前一直玩webkit,不过一直要加载页面太浪费资源了,此时了解到有一个好玩的玩意 phantomjs,这是个可以用命令行来操作webkit的玩意,然后也可以直接在里面用js的api去操作页面(当然,我这边比较简单就懒得用了)。
下载完phantomjs之后直接解压就可以使用,然后在path目录加入phantomjs的路径(以便直接在命令行就可以执行phantomjs命令)。
接下来要完成个代码,一个是用phantomjs去获取页面(采用js编写行为),一个是采用java去调用phantomjs来达到获取内容的作用,接下来直接贴代码。
codes.js
http://localhost/web/?url=http%3A%2F%2Fwww.plusweb.cn
进行测试
/codes.js system = require('system') address = system.args[1];//获得命令行第二个参数 接下来会用到 //console.log('Loading a web page'); var page = require('webpage').create(); var url = address; //console.log(url); page.open(url, function (status) { //Page is loaded! if (status !== 'success') { console.log('Unable to post!'); } else { //console.log(page.content); //var title = page.evaluate(function() { // return document.title;//示范下如何使用页面的jsapi去操作页面的 www.oicqzone.com // }); //console.log(title); //console.log(encodeURIComponent(page.content)); console.log(page.content); } phantom.exit(); });
上述的js代码估计应该没几个看不懂的。。。
接下来贴java代码!
import org.apache.commons.io.IOUtils; import java.io.*; /** * Created with IntelliJ IDEA. * User: lsz * Date: 14-4-22 * Time: 下午1:17 * utils for http */ public class HttpUtils { public static String getAjaxCotnent(String url) throws IOException { Runtime rt = Runtime.getRuntime(); Process p = rt.exec("phantomjs.exe c:/phantomjs/codes.js "+url);//这里我的codes.js是保存在c盘下面的phantomjs目录 InputStream is = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuffer sbf = new StringBuffer(); String tmp = ""; while((tmp = br.readLine())!=null){ sbf.append(tmp); } //System.out.println(sbf.toString()); return sbf.toString(); } public static void main(String[] args) throws IOException { getAjaxCotnent("http://www.plusweb.cn"); } }C#实现代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Net; using System.Threading; namespace phantomjs { class Program { static void Main(string[] args) { // String str = getAjaxCotnent("http://www.plusweb.cn");//测试使用 new Program().listHttp(); } public static String getAjaxCotnent(String url) { ProcessStartInfo start = new ProcessStartInfo(Environment.CurrentDirectory + "//phantomjs//phantomjs.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe start.Arguments = "phantomjs//code.js" + " " + url;//设置命令参数 StringBuilder sb = new StringBuilder(); start.CreateNoWindow = false;//不显示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p = Process.Start(start); string encoding = p.StandardOutput.CurrentEncoding.ToString(); StreamReader reader = p.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 sb.AppendLine(line); while (!reader.EndOfStream) { line = reader.ReadLine(); sb.AppendLine(line); } p.WaitForExit();//等待程序执行完退出进程 p.Close();//关闭进程 reader.Close();//关闭流 string strRet = System.Web.HttpUtility.UrlDecode(sb.ToString()); return strRet; } private void listHttp() { using (HttpListener listerner = new HttpListener()) { string urllister = System.Configuration.ConfigurationSettings.AppSettings["listurl"]; listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问 //listerner.Prefixes.Add(urllister); listerner.Prefixes.Add("http://localhost/web/"); listerner.Start(); Console.WriteLine("WebServer Start Successed......."); while (true) { //等待请求连接 //没有请求则GetContext处于阻塞状态 HttpListenerContext ctx = listerner.GetContext(); ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码 string url = ctx.Request.QueryString["url"]; //使用Writer输出http响应代码 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) { writer.WriteLine(getAjaxCotnent(url)); writer.Close(); ctx.Response.Close(); } } listerner.Stop(); } } } }启动器c#程序访问以下地址:
http://localhost/web/?url=http%3A%2F%2Fwww.plusweb.cn
进行测试
其实原理很简单,就是通过进程间通信用java调用phantomjs这个组件去请求渲染页面,不过这种做法因为每次都要重新启动phantomjs进 程,所以比较慢,还有另外一种直接用phantomjs加载页面后,把内容post给我们自定义的一个http后端接收数据,会更快一点。
phantomjs下载地址:
相关推荐
java调用phantomJs进行网页截图源码,有需要的朋友自行下载
本文主要介绍了C#使用Selenium+PhantomJS抓取数据的方法步骤,具有很好的参考价值,下面跟着小编一起来看下吧
java使用phantomjs把echarts转图片 静态资源
php+phantomjs 采集搜狗微信 在window上跑,完全没问题。里面路径改成自己即可。
java使用phantomjs将网页截长屏存为图片,亲测可用!!!!!!里面有我整理的文档+java源代码!!!!!
使用 phantomjs 和 im4java 生成网页缩略图,切图用的是ImageMagick,附送im4java调用ImageMagick处理图片代码,包括剪切、调整大小、调整到指定比例大小不足部分补白边。包含 phantomjs 包,不包含ImageMagick。
主要介绍了Java利用Phantomjs实现生成图片的功能,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
phantomjs有借鉴有研究。
java+phantomjs+echarts导出图片到服务器,包含JAVA测试类,phantomjs 文件,echarts-convert.js
echarts-java 通过phantomjs java后台生成echats图表,echartspoi相关jar包及phantomjs都在压缩包内
1、将文件夹phantomjs复制到指定的路径下,然后在path增加环境变量,我的是\phantomjs 路径为E:\temp\phantomjs\bin,重启eclipse 即可 2、引入需要的jar文件ECharts-2.2.7.jar 3、ReportUtils.java EnhancedOption....
使用c#对京东搜索页进行价格,名称爬取,使用AngleSharp+Selenium.PhantomJS
该示例在java后台运行,采用phantomjs截取页面存为图片。包括自定义高度全屏截取和div截取两种方式。
首先,我们将Phantom的包引入工程,放在resources目录下。因为我们要保证本地windows开发与服务器linux环境开发打包后都能运行,所以,我们将windows和linux两个包都引入。
简单易懂 java纯后台phantoms生成 echarts图片完整demo,亲测可用!在做医院体检报告打印项目时需用到后台生成图片时需要用到,所以研究了下
配合生成echarts base64
windows版: 1、下载phantomjs,解压到某个目录。...2、将XXX\phantomjs\bin配置到环境变量中(方便使用)。 3、cmd下调用如下命令:oeepdf "url" "output" ,即可生产pdf文件。 url:网页地址 output:pdf输出路径
java部分主要进行集群的管理,任务队列的管理和任务分发,支持jar和class热发布。 phantomjs执行实际的抓取任务,抓取逻辑采用js编写,可以方便的导入jquery, 使用jquery可以很方便地从网页中提取需要的信息,在...
capturejs, 带有PhantomJS和NodeJS的完整网页捕获 命令行 工具 CaptureJS CaptureJS的完整网页捕获命令行工具,带有 PhantomJS 。安装第一个安装 PhantomJS 。$ npm install -g capturejs用法Us