javascript - 在 scrapy 中获取 document.write 的输出

标签 javascript python scrapy

我正在抓取具有以下结构的网站:

<td colspan="1">
    <font class="spy14">
        91.240.61.217
        <script type="text/javascript">document.write("<font class=spy2>:<\/font>"+(One2NineSix^Four3Eight)+(One2NineSix^Four3Eight)+(One2NineSix^Four3Eight)+(Zero9EightSeven^Nine6Six))</script>
</td>

当您在页面上看到它时,它看起来像这样:

91.240.61.217:6660

但是当我尝试抓取它时,我发现自己无法获取上面 JS 脚本生成的“:6660”部分。

如何获取 HTML 的那部分内容?这是我当前的代码:

val = Selector(response)
ip = val.xpath('//tr[@class="spy1xx"]/td[1]/font').extract_first()

JS代码看似无稽之谈,但页面中也有这样的脚本:

eval(function(p,r,o,x,y,s){y=function(c){return(c<r?'':y(parseInt(c/r)))+((c=c%r)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(o--){s[y(o)]=x[o]||y(o)}x=[function(y){return s[y]}];y=function(){return'\\w+'};o=1};while(o--){if(x[o]){p=p.replace(new RegExp('\\b'+y(o)+'\\b','g'),x[o])}}return p}('p=B^C;m=G^I;h=F^E;r=H^J;f=2;l=K^u;e=5;c=4;n=8;t=v^w;j=A^z;g=y^x;k=1;d=D^O;b=T^V;s=6;a=3;o=7;i=0;q=9;L=i^j;Q=k^g;P=f^b;M=a^m;N=c^d;R=e^l;S=s^r;X=o^p;W=n^t;U=q^h;',60,60,'^^^^^^^^^^Four^Seven4Seven^Five^Nine3Three^One^Zero^OneTwoZero^Seven9Four^Two^Nine6Six^Three^TwoOneNine^Eight5One^Seven^Eight^NineThreeTwo^Nine^Four3Eight^Six^Seven0Five^8090^5462^88^808^12035^3129^11286^10218^8909^6038^8888^7449^6689^3320^80^443^2546^Zero9EightSeven^Nine1SixNine^SixThreeFiveFive^8000^Nine7OneZero^Seven0FourOne^ZeroSixTwoEight^One2NineSix^9273^Six2ZeroFour^8118^Eight9SevenThree^FiveFourThreeTwo'.split('\u005e'),0,{}))

所以它确实打印出“:6660”。这是我测试过的。

有什么办法吗

a) 通过 document.write 获取页面上显示的脚本输出

b) 以某种方式抓取 JS 代码并通过 Python 在本地运行它

最佳答案

Guillaume 的答案的替代方法是在您的蜘蛛中使用 js2py 来执行 JS 代码:

>>> import js2py
>>> js = r"eval(function(p,r,o,x,y,s){y=function(c){return(c<r?'':y(parseInt(c/r)))+((c=c%r)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(o--){s[y(o)]=x[o]||y(o)}x=[function(y){return s[y]}];y=function(){return'\\w+'};o=1};while(o--){if(x[o]){p=p.replace(new RegExp('\\b'+y(o)+'\\b','g'),x[o])}}return p}('p=B^C;m=G^I;h=F^E;r=H^J;f=2;l=K^u;e=5;c=4;n=8;t=v^w;j=A^z;g=y^x;k=1;d=D^O;b=T^V;s=6;a=3;o=7;i=0;q=9;L=i^j;Q=k^g;P=f^b;M=a^m;N=c^d;R=e^l;S=s^r;X=o^p;W=n^t;U=q^h;',60,60,'^^^^^^^^^^Four^Seven4Seven^Five^Nine3Three^One^Zero^OneTwoZero^Seven9Four^Two^Nine6Six^Three^TwoOneNine^Eight5One^Seven^Eight^NineThreeTwo^Nine^Four3Eight^Six^Seven0Five^8090^5462^88^808^12035^3129^11286^10218^8909^6038^8888^7449^6689^3320^80^443^2546^Zero9EightSeven^Nine1SixNine^SixThreeFiveFive^8000^Nine7OneZero^Seven0FourOne^ZeroSixTwoEight^One2NineSix^9273^Six2ZeroFour^8118^Eight9SevenThree^FiveFourThreeTwo'.split('\u005e'),0,{}))"
>>> js2py.eval_js(js)
16296

Js2Py:https://github.com/PiotrDabkowski/Js2Py

至于从页面中提取 JS 代码,恐怕您需要分享有关页面本身的更多详细信息,以便我们可以帮助您弄清楚如何做。

关于javascript - 在 scrapy 中获取 document.write 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500652/

相关文章:

python - 在 Sagemath 中积分并绘制分段函数

python - python中的多线程是一个神话吗?

python - MongoDB 无效文档 : Cannot encode object

python - scrapy_redis 在空闲 x 时间后停止我的蜘蛛

javascript - 如何运行从 github fork 的 Node.JS 应用程序?

javascript - 如何调用ng-keyup中的多个函数?

python - 交换 numpy 矩阵中的列

python - 仅使用 scrapy 获取一行输出到 json 文件

javascript - 尝试使用递归添加数字时得到 NaN

javascript - Canvas 中的 HTML5 图像适配问题