python使用execjs执行含有document、window等对象的js代码,使用jsdom解决

当我们分析爬虫时,我们有时会遇到一些加密参数,所以我们需要对js进行反向分析。

[En]

When we analyze the crawler, we sometimes encounter some encryption parameters, so we need to reverse analyze the js.

python执行js有一些第三方库

https://www.jianshu.com/p/2da6f6ad01f0

因为我用的是Python3,所以没有用到PyV8(网上安装的方法,而且我安装得不好,可能是我太傻了!(如果有什么麻烦,请给我一些建议)

[En]

Because I use Python3, so I didn’t use PyV8 (the method of installing on the Internet, and I didn’t install it well, maybe I’m too stupid! If there is any trouble, please give me some advice)

我更经常使用js2py和execjs。

[En]

I use js2py and execjs more often.

有一次在分析js时,我已经找到了相应的加密方法。当我在浏览器上运行它时,我直接得到了结果,一个令人满意的结果。

[En]

Once when I was analyzing js, I had found the corresponding encryption method. When I ran it on the browser, I got the result directly, a happy one.

然后我复制了js代码,在Python中通过js2py运行它,发现了一个错误,并说

[En]

Then I copied the js code, ran it through js2py in Python, found an error and said

ReferenceError: window is not defined

当我想到这个浏览器中的对象时,我会感到绝望,然后我想知道execjs能否突破这一点,因为它依赖节点来执行。

[En]

I feel desperate when I think of the objects in this browser, and then I wonder if execjs can break through this because it relies on node to execute.

结果仍然不起作用,因为您只能在节点中运行js语法,并且没有窗口和文档等对象。

[En]

The result still doesn’t work, because you can only run js syntax in node, and there are no objects such as window and document.

后来,一个偶然的机会,我们发现节点中有一个库jsdom,它可以生成相应的环境。

[En]

Later, by chance, we found that there is a library jsdom in node, which can generate the corresponding environment.

https://www.npmjs.com/package/jsdom

全局安装命令

[En]

Global installation command

npm i jsdom -g

python使用execjs执行含有document、window等对象的js代码,使用jsdom解决
python使用execjs执行含有document、window等对象的js代码,使用jsdom解决

你可以看到,进口后,有各种各样的环境,非常完美。

[En]

You can see that after the import, there are all kinds of environments, perfect.

因此,我们只需在相应的js代码前面添加以下代码即可正常工作。

[En]

So we just need to add the following code to the front of the corresponding js code to work properly.

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(<!DOCTYPE html><p>Hello world</p>);
window = dom.window;
document = window.document;
XMLHttpRequest = window.XMLHttpRequest;

全局安装jsdom后,在node中如上编写没有问题,但如果我们想在python中使用它,我们不能全局安装它。

[En]

After installing jsdom globally, there is no problem with writing it as above in node, but if we want to use it in python, we cannot install it globally.

如果全局安装,则在使用时会报告以下错误,即找不到jsdom

[En]

If you install globally, the following error will be reported when using it, saying that jsdom could not be found

execjs._exceptions.ProgramError: Error: Cannot find module 'jsdom'

有两种解决方案。

[En]

There are two solutions.

1.就是在python执行文件所在的运行目录下,使用npm安装jsdom
2. 使用cwd参数,指定模块的所在目录,比如,我们在全局安装的jsdom,在cmd里通过npm root -g 可以查看全局模块安装路径: C:\Users\w001\AppData\Roaming\npm\node_modules
当我们使用它时,代码可以写成如下

[En]

When we use it, the code can be written as follows

import execjs
with open(r'&#x8981;&#x8FD0;&#x884C;&#x7684;.js','r',encoding='utf-8') as f:
    js = f.read()
ct = execjs.compile(js,cwd=r'C:\Users\w001\AppData\Roaming\npm\node_modules')
print(ct.call('Rohr_Opt.reload','1'))

Original: https://www.cnblogs.com/huchong/p/11044238.html
Author: 听风。
Title: python使用execjs执行含有document、window等对象的js代码,使用jsdom解决

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6217/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部