失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > phantomjs搭建输出js渲染后的html接口

phantomjs搭建输出js渲染后的html接口

时间:2020-07-09 11:55:32

相关推荐

phantomjs搭建输出js渲染后的html接口

接触过爬虫的人都知道,有很多html是js渲染后才有数据给予解析的,php执行JavaScript可以有V8 javascript engine,要想获取渲染后的html也可以直接使用

php的扩展 querylist 采集,文档:

/docs/guide/v4/PhantomJS

不过这里想聊聊phantomjs的api接口文档,以前只会命令行模式的phantomjs指令 + php的exec函数执行JavaScript代码

phantomjs的api:

/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();}} else if(opera == 'parseJs') {// 解析JSpage.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();});} });

View Code

注意监听的端口不要已经被使用了哦

如果觉得《phantomjs搭建输出js渲染后的html接口》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。