Python執(zhí)行js字符串常見方法示例
方法
執(zhí)行大型js時(shí)有點(diǎn)慢
特殊編碼的輸入或輸出參數(shù)會(huì)出現(xiàn)報(bào)錯(cuò),解決方法:
可以把輸入或輸出的參數(shù)用base64編碼一下。base64都是英文和數(shù)字,沒有特殊字符了
1--js2py
pip insatll js2py
# 獲取執(zhí)行JS的環(huán)境
context = js2py.EvalJs()
# 加載執(zhí)行
context.execute('放JS字符代碼')
2--execjs
import execjs print(execjs.get().name) # Node.js (V8)
import execjs
user_id = '3232597584'
url = f'https://www.toutiao.com/toutiao/api/pc/feed/?min_behot_time=1588149898&category=__all__&utm_source=toutiao&widen=1&tadrequire=true&user_id={user_id}&visited_uid={user_id}'
js_index = """
js
"""
# 打開js文件讀取
# new_url = execjs.compile(open("/Users/ts/Desktop/Jeff/今日頭條/new_sign.js").read()).call('get_sigtrue', url)
# 讀取js字符串
new_url = execjs.compile(js_index).call('get_sigtrue', url)
print(new_url)
3--execjs
import execjs
def get_js():
f = open("./new_sign.js", 'r', encoding = 'UTF-8')
line = f.readline()
htmlstr = ''
while line:
htmlstr = htmlstr + line
line = f.readline()
return htmlstr
jsstr = get_js()
ctx = execjs.compile(jsstr)
user_id = '3232597584'
url = f'https://www.toutiao.com/toutiao/api/pc/feed/?min_behot_time=1588149898&category=__all__&utm_source=toutiao&widen=1&tadrequire=true&user_id={user_id}&visited_uid={user_id}'
print(ctx.call('getUrl', url))以上就是Python執(zhí)行js字符串常見方法示例的詳細(xì)內(nèi)容,更多關(guān)于Python執(zhí)行js字符串的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript String 的擴(kuò)展方法集合
String 的擴(kuò)展方法集合,可以是javascript對(duì)string的功能更多2008-06-06
JavaScript是如何實(shí)現(xiàn)繼承的(六種方式)
大多OO語言都支持兩種繼承方式: 接口繼承和實(shí)現(xiàn)繼承 ,而ECMAScript中無法實(shí)現(xiàn)接口繼承,ECMAScript只支持實(shí)現(xiàn)繼承,而且其實(shí)現(xiàn)繼承主要是依靠原型鏈來實(shí)現(xiàn),下文給大家技術(shù)js實(shí)現(xiàn)繼承的六種方式,需要的朋友參考下2016-03-03
js自定義方法通過隱藏iframe實(shí)現(xiàn)文件下載
通過隱藏iframe實(shí)現(xiàn)文件下載,不可思議吧,但確實(shí)是可以實(shí)現(xiàn)的,不懂的看下代碼,或許可以幫助到你,而且此功能是用js實(shí)現(xiàn)的,感興趣的你可不要錯(cuò)過了哈2013-02-02

