py调用js代码
使用 PyExecJS 模块!pyexecjs 是一个可以使用python来模拟运行javaScript的库。我们需要安装 pip install pyexecjs 对其进行环境安装
- 注意本机电脑系统需要安装nodejs
如何使用
- 将调式好的js代码存放到js源文件中进行保存
在py文件中引入ExecJS 模块
import execjsnode = execjs.get()
对js源文件进行编译
file='index.js'ctx =node.compile(open(file,encoding='utf-8').read())
调用编译好的js文件中的指定函数
result = ctx.eval('getPwd("123456")')print(result)
完整示列
```python import execjs node = execjs.get()
file = ‘index.js’ ctx = node.compile(open(file,encoding=’utf-8’).read())
result = ctx.eval(‘getPwd(“123456”)’) print(result)
<a name="Sr7Um"></a>## node 调用python 函数1. node 向 python 函数传递参数2. python 函数中接收参数 返回数据到node 是通过 print() 打印来返回的```javascriptvar srcImgUrl="114.117.234.211:5970";var Urlid="dca1171e4a140b6564074931d67e1cb6"//异步执行exec('python ./web.py' + srcImgUrl + ' ' + Urlid, {encoding: 'utf8'},function(error,stdout,stderr){if(error) {console.info('stderr : '+stderr);}console.log('exec: ' + stdout);})//同步执行let output = execSync('python ./web.py ' + srcImgUrl + ' ' + Urlid, {encoding: 'utf8'})console.log(output.toString());
import sysimport requestsdef geterweima(c_url, id):# url = "http://localhost:3002/erci?urlhost=" + c_url + "&id="+id# url = "http://114.117.234.211:5970/wx/get?c=tz&id=dca1171e4a140b6564074931d67e1cb6"url = "http://"+ c_url +"/wx/get?c=tz&id="+idheaders = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50",}try:response = requests.get(url, headers=headers, timeout=5)if response.status_code == 200:print(response.text)except Exception:print("2")geterweima(sys.argv[1],sys.argv[2])
