phantomjs搭建输出js渲染后的html接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了phantomjs搭建输出js渲染后的html接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3703字,纯文字阅读大概需要6分钟。
内容图文
接触过爬虫的人都知道,有很多html是js渲染后才有数据给予解析的,php执行JavaScript可以有V8 javascript engine,要想获取渲染后的html也可以直接使用
php的扩展 querylist 采集,文档:
https://www.querylist.cc/docs/guide/v4/PhantomJS
不过这里想聊聊phantomjs的api接口文档,以前只会命令行模式的phantomjs指令 + php的exec函数执行JavaScript代码
phantomjs的api:
http://phantomjs.org/api/
接口功能:提供一个接口地址,传入网址返回渲染后的html(字符串)
使用webserver模式:phantomjs可执行文件执行js文件,就会监听端口,当有请求进来之后就会返回渲染后的html
var webserver = require(‘webserver‘); var server = webserver.create(); var pageInfo = {}; var action = {‘openUrl‘:true,‘parseJs‘:true}; var service = server.listen(8888, function(request, response) { post = request.post; var opera = (typeof post.action != ‘undefined‘ && typeof action[post.action] != ‘undefined‘) ? post.action : ‘openUrl‘; var webPage = require(‘webpage‘); var page = webPage.create(); if(opera == ‘openUrl‘) {// 打开URL页面 pageInfo[‘url‘] = (typeof post.url != ‘undefined‘) ? post.url : ‘‘; pageInfo[‘postData‘] = (typeof post.postData != ‘undefined‘) ? post.postData : ‘‘; pageInfo[‘loadSource‘] = (typeof post.loadSource != ‘undefined‘ && post.loadSource == ‘no‘) ? false : true; var body = ‘‘; if(pageInfo[‘loadSource‘] === false) { page.settings.javascriptEnabled = false; page.settings.loadImages = false; } if(pageInfo.url) { page.open(pageInfo.url, function(status) { body = page[‘content‘]; console.log(‘url:‘+pageInfo.url); console.log(‘Status: ‘ + status); response.statusCode = 200; response.write(body); response.close(); }) } else { response.statusCode = 200; response.write(‘请传URL‘); response.close(); } } elseif(opera == ‘parseJs‘) {// 解析JS page.open(‘‘, function(status) { var code = "eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!‘‘.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return‘\\w+‘};c=1};while(c--){if(k[c]){p=p.replace(new RegExp(‘\\b‘+e(c)+‘\\b‘,‘g‘),k[c])}}return p}(‘e i={\‘g\‘:6,\‘l\‘:\‘豪门第一盛婚\‘,\‘f\‘:\‘/6/k\‘,\‘d\‘:\‘/6/8\‘,\‘7\‘:\‘/6/9\‘,\‘a\‘:\‘0话\‘,\‘c\‘:[\‘/3/2/h/5/0/b.4\‘,\‘/3/2/h/5/0/j.4\‘,\‘/3/2/h/5/0/w.4\‘,\‘/3/2/h/5/0/v.4\‘,\‘/3/2/h/5/0/u.4\‘,\‘/3/2/h/5/0/m.4\‘,\‘/3/2/h/5/0/x.4\‘,\‘/3/2/h/5/0/y.4\‘,\‘/3/2/h/5/0/t.4\‘,\‘/3/2/h/5/0/s.4\‘,\‘/3/2/h/5/0/o.4\‘],\‘n\‘:p,\‘q\‘:\‘/6/\‘,\‘r\‘:1};‘,35,35,‘116||tuku|ManHuaKu|jpg|haomendiyishenghun|25651|pcid|0120|0118|ctitle|1612|fs|ncid|var|cid|bid||cInfo|1613|0119|btitle|1617|fc|kz5thyicgkd1622|11|burl|bs|xuaghwz33ar1621|1620|1616|1615|1614|1618|1619‘.split(‘|‘),0,{}))"; code = code.replace(/^eval/, ‘‘); var title = page.evaluate(function(s) { return eval("(function(p,a,c,k,e,d){e=function(c){return(c<a?‘‘:e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!‘‘.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return‘\\w+‘};c=1};while(c--)if(k[c])p=p.replace(new RegExp(‘\\b‘+e(c)+‘\\b‘,‘g‘),k[c]);return p}(‘x l={\‘q\‘:f,\‘j\‘:\‘豪门第一盛婚\‘,\‘m\‘:\‘/f/3\‘,\‘u\‘:\‘/f/2\‘,\‘v\‘:\‘/f/0\‘,\‘n\‘:\‘5话\‘,\‘p\‘:[\‘/g/w/q/r/5/6.s\‘,\‘/g/w/q/r/5/7.s\‘,\‘/g/w/q/r/5/8.s\‘,\‘/g/w/q/r/5/9.s\‘,\‘/g/w/q/r/5/a.s\‘,\‘/g/w/q/r/5/b.s\‘,\‘/g/w/q/r/5/c.s\‘,\‘/g/w/q/r/5/d.s\‘,\‘/g/w/q/r/5/e.s\‘,\‘/g/w/q/r/5/y.s\‘,\‘/g/w/q/r/5/t.s\‘],\‘o\‘:4,\‘k\‘:\‘/f/\‘,\‘i\‘:3};‘,62,35,‘0118|0119|0120|1|11|116|1612|1613|1614|1615|1616|1617|1618|1619|1620|25651|ManHuaKu|bid|bs|btitle|burl|cInfo|cid|ctitle|fc|fs|h|haomendiyishenghun|jpg|kz5thyicgkd1622|ncid|pcid|tuku|var|xuaghwz33ar1621‘.split(‘|‘),0,{}))"); }, code); //console.log(code) console.log(title) //response.statusCode = 200;//response.write(eval(code));//response.close(); phantom.exit(); }); } });
注意监听的端口不要已经被使用了哦
原文:https://www.cnblogs.com/xuweiqiang/p/10361088.html
内容总结
以上是互联网集市为您收集整理的phantomjs搭建输出js渲染后的html接口全部内容,希望文章能够帮你解决phantomjs搭建输出js渲染后的html接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。