使用PyV8在Python爬虫中执行js代码


Posted in Python onFebruary 16, 2017

前言

可能很多人会觉得这是一个奇葩的需求,爬虫去好好的爬数据不就行了,解析js干嘛?吃饱了撑的?

搜索一下互联网上关于这个问题还真不少,但是大多数童鞋是因为自己的js基础太烂,要么是HTML基础烂,要么ajax基础烂,反正各方面都很烂。基础这么渣不好好去学基础写什么爬虫?

那你肯定要问了“请问我的朋友,你TM怎么也有这个需求?莫非你是个技术渣?”

非也非也,博主作为一个拥有3年多前端经验的攻城尸,怎么会被这个问题给难倒呢,老夫今天遇到的问题很显然没有那么简单。

问题

那么博主到底是遇到什么问题了呢?

博主今天要去爬一个接口,但是调用那个接口需要带上令牌,也就是存储在Cookie中的一个类似token的东西,Cookie的值是一段js生成的,这段js又是通过另外一个接口获取回来的,而获取回来的js代码还是动态的,WTF!!!开发人员你这是 弄撒嘞?

路人甲:我擦嘞,声称经验老道的博主不会分析js的逻辑?

对,我就是不会,特么的js代码都是混淆加密的,眼睛都看瞎了都特么不知道写的都是写啥?

算了,我直接执行拿到结果就好了,管他写的是什么鬼。

思路

理一理思路,现在要做的事情其实很简单

  1. 请求接口A,拿到动态生成的混淆过的js代码
  2. 执行js代码,拿到生成的cookie值
  3. 请求接口B,带上js生成的令牌
  4. 拿到结果,愉快的玩耍...

思路相当的清晰,感觉秒秒钟就可以实现了呢。()

难题

Python里面执行js?有点意思,我干嘛不用nodejs呢?

因为Python是世界上最?诺挠镅园。∶挥兄?唬?/p>

找到了PyV8这个神奇的模块,机器已经有了pip,执行安装一下不就OK了?

pip install pyv8

不要怀疑,博主机器装的是 Kali Linux ,Root 权限,不需要 sudo

接着报错

pip install -U PyV8
Collecting PyV8
 Using cached PyV8-0.5.zip
Building wheels for collected packages: PyV8
 Running setup.py bdist_wheel for PyV8 ... error
 Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-QUm4bX/PyV8/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmpb0udlepip-wheel- --python-tag cp27:
 running bdist_wheel
 running build
 running build_py
 creating build
 creating build/lib.linux-x86_64-2.7
 copying PyV8.py -> build/lib.linux-x86_64-2.7
 running build_ext
 building '_PyV8' extension
 creating build/temp.linux-x86_64-2.7
 creating build/temp.linux-x86_64-2.7/src
 x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-cFt4xx/python2.7-2.7.12=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DBOOST_PYTHON_STATIC_LIB -Ilib/python/inc -Ilib/boost/inc -Ilib/v8/inc -I/usr/include/python2.7 -c src/Exception.cpp -o build/temp.linux-x86_64-2.7/src/Exception.o
 cc1plus: warning: command line option ‘-Wstrict-prototypes' is valid for C/ObjC but not for C++
 In file included from src/Exception.cpp:1:0:
 src/Exception.h:6:16: fatal error: v8.h: 没有那个文件或目录
 #include <v8.h>
     ^
 compilation terminated.
 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
 
 ----------------------------------------
 Failed building wheel for PyV8
 Running setup.py clean for PyV8
Failed to build PyV8
Installing collected packages: PyV8
 Running setup.py install for PyV8 ... error
 Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-QUm4bX/PyV8/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-7OAwUa-record/install-record.txt --single-version-externally-managed --compile:
 running install
 running build
 running build_py
 creating build
 creating build/lib.linux-x86_64-2.7
 copying PyV8.py -> build/lib.linux-x86_64-2.7
 running build_ext
 building '_PyV8' extension
 creating build/temp.linux-x86_64-2.7
 creating build/temp.linux-x86_64-2.7/src
 x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-cFt4xx/python2.7-2.7.12=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DBOOST_PYTHON_STATIC_LIB -Ilib/python/inc -Ilib/boost/inc -Ilib/v8/inc -I/usr/include/python2.7 -c src/Exception.cpp -o build/temp.linux-x86_64-2.7/src/Exception.o
 cc1plus: warning: command line option ‘-Wstrict-prototypes' is valid for C/ObjC but not for C++
 In file included from src/Exception.cpp:1:0:
 src/Exception.h:6:16: fatal error: v8.h: 没有那个文件或目录
  #include <v8.h>
     ^
 compilation terminated.
 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
 
 ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-QUm4bX/PyV8/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-7OAwUa-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-QUm4bX/PyV8/

似乎是因为缺少 v8.h 这个文件导致的,可是又看不懂啥意思。

解决

通过搜索引擎找到了解决方案,原来是因为 PyV8 依赖于Boost ,然而这个问题官方并没有说,所以得先安装下这个包

apt-get update && apt-get install libboost-all-dev

安装完成之后继续安装 PyV8 ,依然是上面同样的问题,看来只能手动来了。

下载 https://github.com/emmetio/pyv8-binaries

解压并选择合适自己系统环境的文件,再次解压 并把解压得到的文件复制到

/usr/lib/python2.7/dist-packages/

里面去,然后测试看是否成功,终端执行

python
import PyV8

如果没有报错,那就成功了,开始愉快的玩耍,下面是我需要解析的js代码

var l = [119, 98, 115, 33, 111, 109, 120, 105, 118, 62, 92, 50, 50, 54, 45, 50, 50, 51, 45, 50, 50, 55, 45, 50, 49, 58, 45, 50, 50, 49, 45, 50, 51, 51, 45, 50, 50, 52, 45, 50, 50, 51, 45, 50, 50, 54, 45, 50, 49, 55, 45, 50, 49, 58, 45, 50, 49, 50, 45, 50, 50, 54, 45, 50, 50, 58, 45, 50, 50, 49, 45, 50, 50, 51, 45, 50, 50, 58, 45, 50, 51, 51, 45, 50, 50, 58, 45, 50, 50, 55, 45, 50, 50, 54, 45, 50, 50, 54, 94, 60, 119, 98, 115, 33, 121, 119, 99, 100, 108, 62, 92, 49, 45, 51, 50, 45, 53, 45, 55, 45, 50, 50, 45, 57, 45, 56, 45, 50, 51, 45, 51, 45, 51, 49, 45, 50, 52, 45, 50, 54, 45, 50, 49, 45, 50, 57, 45, 52, 45, 58, 45, 50, 53, 45, 50, 56, 45, 54, 45, 50, 55, 45, 50, 58, 45, 50, 94, 60, 119, 98, 115, 33, 118, 62, 35, 35, 60, 103, 112, 115, 33, 41, 119, 62, 49, 60, 119, 61, 121, 119, 99, 100, 108, 47, 109, 102, 111, 104, 117, 105, 60, 119, 44, 44, 42, 124, 118, 44, 62, 84, 117, 115, 106, 111, 104, 47, 103, 115, 112, 110, 68, 105, 98, 115, 68, 112, 101, 102, 41, 111, 109, 120, 105, 118, 92, 121, 119, 99, 100, 108, 92, 119, 94, 94, 42, 126, 60, 37, 47, 100, 112, 112, 108, 106, 102, 41, 40, 114, 117, 112, 108, 102, 111, 40, 45, 118, 45, 124, 113, 98, 117, 105, 59, 40, 48, 40, 126, 42, 60];
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
}('6 3=\'\';7(2=0;2<4.5;2++){3+=8.a(4[2]-1)};9(3)', 11, 11, '||i|t|l|length|var|for|String|eval|fromCharCode'.split('|'), 0, {}))

已经经过整理,其实刚开始就只有一行,比较尴尬

姿势

折腾的过程可谓是各种曲折,不过也学到了不少姿势,比如,如何把混淆的js还原成原始代码

使用Firebug插件就能轻松解决这个问题,打开firebug插件,找到脚本选项,选择带 eval 的项,一般解析到最后一行就是原始代码了,我上面的那段 js 还原之后就便成了这个样子

var balwi=[115,116,115,122,112,115,110,106,122,110,122,112,101,119,115,106,113,101,116,116,119,106];var ljpry=[15,21,4,9,12,14,11,0,18,20,8,16,7,2,1,10,17,13,19,6,5,3];var j="";for (k=0;k<ljpry.length;k++){j+=String.fromCharCode(balwi[ljpry[k]])};$.cookie('qtoken',j,{path:'/'});

稍微整理一下得到一个格式清晰的代码

var balwi = [115, 116, 115, 122, 112, 115, 110, 106, 122, 110, 122, 112, 101, 119, 115, 106, 113, 101, 116, 116, 119, 106];
var ljpry = [15, 21, 4, 9, 12, 14, 11, 0, 18, 20, 8, 16, 7, 2, 1, 10, 17, 13, 19, 6, 5, 3];
var j = "";
for (k = 0; k < ljpry.length; k++) {
 j += String.fromCharCode(balwi[ljpry[k]])
};
$.cookie('qtoken', j, {
 path: '/'
});

有了原始代码就很容易得到令牌的生成算法,使用Python生成,这回不用麻烦 PyV8 大神出马了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

Python 相关文章推荐
python操作CouchDB的方法
Oct 08 Python
Python函数中定义参数的四种方式
Nov 30 Python
在Python的Django框架中更新数据库数据的方法
Jul 17 Python
Python3字符串学习教程
Aug 20 Python
python实现可以断点续传和并发的ftp程序
Sep 13 Python
python3 requests中使用ip代理池随机生成ip的实例
May 07 Python
python3使用腾讯企业邮箱发送邮件的实例
Jun 28 Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 Python
18个Python脚本可加速你的编码速度(提示和技巧)
Oct 17 Python
python3 tkinter实现添加图片和文本
Nov 26 Python
PyQt5中QSpinBox计数器的实现
Jan 18 Python
Python Matplotlib绘制动画的代码详解
May 30 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
Feb 16 #Python
Python变量和数据类型详解
Feb 15 #Python
Django学习笔记之Class-Based-View
Feb 15 #Python
关于Python面向对象编程的知识点总结
Feb 14 #Python
Django日志模块logging的配置详解
Feb 14 #Python
深入理解Python对Json的解析
Feb 14 #Python
Python 2与Python 3版本和编码的对比
Feb 14 #Python
You might like
服务器web工具 php环境下
2010/12/29 PHP
PHP header()函数使用详细(301、404等错误设置)
2013/04/17 PHP
深入PHP内存相关的功能特性详解
2013/06/08 PHP
php实现读取超大文件的方法
2014/07/28 PHP
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
2014/12/09 PHP
在CentOS上搭建LAMP+vsftpd环境的简单指南
2015/08/01 PHP
php下载文件超时时间的设置方法
2016/10/06 PHP
PHP+MYSQL实现读写分离简单实战
2017/03/13 PHP
php中各种定义变量的方法小结
2017/10/18 PHP
Jquery 表单验证类介绍与实例
2013/06/09 Javascript
jQuery中andSelf()方法用法实例
2015/01/08 Javascript
javascript实现时间格式输出FormatDate函数
2015/01/13 Javascript
原生js模拟淘宝购物车项目实战
2015/11/18 Javascript
js实现文字选中分享功能
2017/01/25 Javascript
详解前端路由实现与react-router使用姿势
2017/08/07 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
使用JS判断页面是首次被加载还是刷新
2019/05/26 Javascript
js实现登录时记住密码的方法分析
2020/04/05 Javascript
[59:35]DOTA2-DPC中国联赛定级赛 Aster vs DLG BO3第一场 1月8日
2021/03/11 DOTA
Python制作Windows系统服务
2017/03/25 Python
磁盘垃圾文件清理器python代码实现
2020/08/24 Python
Python闭包思想与用法浅析
2018/12/27 Python
解决python3运行selenium下HTMLTestRunner报错的问题
2018/12/27 Python
Python实现查找数组中任意第k大的数字算法示例
2019/01/23 Python
Python列表切片操作实例总结
2019/02/19 Python
利用pyecharts实现地图可视化的例子
2019/08/12 Python
python PyQt5/Pyside2 按钮右击菜单实例代码
2019/08/17 Python
解决python 3 urllib 没有 urlencode 属性的问题
2019/08/22 Python
用CSS3打造HTML5的Logo(实现代码)
2016/06/16 HTML / CSS
英国女士家居服网站:hush
2017/08/09 全球购物
伦敦哈德森鞋:Hudson Shoes
2018/02/06 全球购物
世嘉游戏英国官方商店:SEGA Shop UK
2019/09/20 全球购物
大型会议接待方案
2014/03/01 职场文书
学习2014年全国两会心得体会
2014/03/12 职场文书
危爆物品安全大检查大整治工作方案
2014/05/03 职场文书
小学安全工作汇报材料
2014/08/19 职场文书