欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用PyV8在Python爬蟲中執(zhí)行js代碼

 更新時(shí)間:2017年02月16日 09:35:47   作者:一只猿  
PyV8是chrome用來執(zhí)行javascript的引擎,據(jù)說是最快的js引擎,通過pyv8的封裝,可以在python中使用。下面這篇文章主要介紹了使用PyV8在Python爬蟲中執(zhí)行js代碼的相關(guān)資料,需要的朋友可以參考下。

前言

可能很多人會(huì)覺得這是一個(gè)奇葩的需求,爬蟲去好好的爬數(shù)據(jù)不就行了,解析js干嘛?吃飽了撐的?

搜索一下互聯(lián)網(wǎng)上關(guān)于這個(gè)問題還真不少,但是大多數(shù)童鞋是因?yàn)樽约旱膉s基礎(chǔ)太爛,要么是HTML基礎(chǔ)爛,要么ajax基礎(chǔ)爛,反正各方面都很爛?;A(chǔ)這么渣不好好去學(xué)基礎(chǔ)寫什么爬蟲?

那你肯定要問了“請(qǐng)問我的朋友,你TM怎么也有這個(gè)需求?莫非你是個(gè)技術(shù)渣?”

非也非也,博主作為一個(gè)擁有3年多前端經(jīng)驗(yàn)的攻城尸,怎么會(huì)被這個(gè)問題給難倒呢,老夫今天遇到的問題很顯然沒有那么簡單。

問題

那么博主到底是遇到什么問題了呢?

博主今天要去爬一個(gè)接口,但是調(diào)用那個(gè)接口需要帶上令牌,也就是存儲(chǔ)在Cookie中的一個(gè)類似token的東西,Cookie的值是一段js生成的,這段js又是通過另外一個(gè)接口獲取回來的,而獲取回來的js代碼還是動(dòng)態(tài)的,WTF?。?!開發(fā)人員你這是 弄撒嘞?

路人甲:我擦嘞,聲稱經(jīng)驗(yàn)老道的博主不會(huì)分析js的邏輯?

對(duì),我就是不會(huì),特么的js代碼都是混淆加密的,眼睛都看瞎了都特么不知道寫的都是寫啥?

算了,我直接執(zhí)行拿到結(jié)果就好了,管他寫的是什么鬼。

思路

理一理思路,現(xiàn)在要做的事情其實(shí)很簡單

  1. 請(qǐng)求接口A,拿到動(dòng)態(tài)生成的混淆過的js代碼
  2. 執(zhí)行js代碼,拿到生成的cookie值
  3. 請(qǐng)求接口B,帶上js生成的令牌
  4. 拿到結(jié)果,愉快的玩耍...

思路相當(dāng)?shù)那逦?,感覺秒秒鐘就可以實(shí)現(xiàn)了呢。()

難題

Python里面執(zhí)行js?有點(diǎn)意思,我干嘛不用nodejs呢?

因?yàn)镻ython是世界上最屌的語言??!沒有之一!

找到了PyV8這個(gè)神奇的模塊,機(jī)器已經(jīng)有了pip,執(zhí)行安裝一下不就OK了?

pip install pyv8

不要懷疑,博主機(jī)器裝的是 Kali Linux ,Root 權(quán)限,不需要 sudo

接著報(bào)錯(cuò)

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: 沒有那個(gè)文件或目錄
 #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: 沒有那個(gè)文件或目錄
  #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/

似乎是因?yàn)槿鄙?v8.h 這個(gè)文件導(dǎo)致的,可是又看不懂啥意思。

解決

通過搜索引擎找到了解決方案,原來是因?yàn)?PyV8 依賴于Boost ,然而這個(gè)問題官方并沒有說,所以得先安裝下這個(gè)包

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

安裝完成之后繼續(xù)安裝 PyV8 ,依然是上面同樣的問題,看來只能手動(dòng)來了。

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

解壓并選擇合適自己系統(tǒng)環(huán)境的文件,再次解壓 并把解壓得到的文件復(fù)制到

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

里面去,然后測試看是否成功,終端執(zhí)行

python
import PyV8

如果沒有報(bào)錯(cuò),那就成功了,開始愉快的玩耍,下面是我需要解析的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, {}))

已經(jīng)經(jīng)過整理,其實(shí)剛開始就只有一行,比較尷尬

姿勢(shì)

折騰的過程可謂是各種曲折,不過也學(xué)到了不少姿勢(shì),比如,如何把混淆的js還原成原始代碼

使用Firebug插件就能輕松解決這個(gè)問題,打開firebug插件,找到腳本選項(xiàng),選擇帶 eval 的項(xiàng),一般解析到最后一行就是原始代碼了,我上面的那段 js 還原之后就便成了這個(gè)樣子

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:'/'});

稍微整理一下得到一個(gè)格式清晰的代碼

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 大神出馬了。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • 在python中實(shí)現(xiàn)調(diào)用可執(zhí)行文件.exe的3種方法

    在python中實(shí)現(xiàn)調(diào)用可執(zhí)行文件.exe的3種方法

    今天小編就為大家分享一篇在python中實(shí)現(xiàn)調(diào)用可執(zhí)行文件.exe的3種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單實(shí)例

    Python實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-03-03
  • Python 列表list使用介紹

    Python 列表list使用介紹

    這篇文章主要介紹了Python 列表list使用介紹,需要的朋友可以參考下
    2014-11-11
  • Python 中對(duì) XML 文件的編碼轉(zhuǎn)換問題

    Python 中對(duì) XML 文件的編碼轉(zhuǎn)換問題

    這篇文章主要介紹了Python 中對(duì) XML 文件的編碼轉(zhuǎn)換問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • python中csv文件的若干讀寫方法小結(jié)

    python中csv文件的若干讀寫方法小結(jié)

    今天小編就為大家分享一篇python中csv文件的若干讀寫方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • OpenCV+Python幾何變換的實(shí)現(xiàn)示例

    OpenCV+Python幾何變換的實(shí)現(xiàn)示例

    這篇文章主要介紹了OpenCV+Python幾何變換的實(shí)現(xiàn)示例,圖像的幾何變換是指將一幅圖像映射到另一幅圖像內(nèi)。有縮放、翻轉(zhuǎn)、仿射變換、透視、重映射等操作。感興趣的可以了解一下
    2021-03-03
  • 一文掌握python中的__init__的意思及使用場景分析

    一文掌握python中的__init__的意思及使用場景分析

    __init__是構(gòu)造方法,誰調(diào)用,表示誰(更直觀的理解就是類的方法中,誰調(diào)用,表示誰,見下面第一個(gè)代碼)!!并不是必選項(xiàng),也就是說在類中,這個(gè)不是必須用的,那什么場景需要用到,什么場景不需要用到呢,感興趣的朋友跟隨小編一起看看吧
    2023-02-02
  • Django如何配置mysql數(shù)據(jù)庫

    Django如何配置mysql數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了Django配置mysql數(shù)據(jù)庫的詳細(xì)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Python玩轉(zhuǎn)Excel的讀寫改實(shí)例

    Python玩轉(zhuǎn)Excel的讀寫改實(shí)例

    今天小編就為大家分享一篇關(guān)于Python玩轉(zhuǎn)Excel的讀寫改實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 詳解python3中tkinter知識(shí)點(diǎn)

    詳解python3中tkinter知識(shí)點(diǎn)

    本篇文章給大家分享了關(guān)于python3中tkinter的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,有興趣的朋友參考下。
    2018-06-06

最新評(píng)論