Python執(zhí)行js字符串常見方法示例
更新時間:2022年04月12日 11:19:35 作者:Jeff的技術棧
這篇文章主要為大家介紹了Python執(zhí)行js字符串常見方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
方法
執(zhí)行大型js時有點慢
特殊編碼的輸入或輸出參數(shù)會出現(xiàn)報錯,解決方法:
可以把輸入或輸出的參數(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字符串常見方法示例的詳細內(nèi)容,更多關于Python執(zhí)行js字符串的資料請關注腳本之家其它相關文章!
相關文章
JavaScript是如何實現(xiàn)繼承的(六種方式)
大多OO語言都支持兩種繼承方式: 接口繼承和實現(xiàn)繼承 ,而ECMAScript中無法實現(xiàn)接口繼承,ECMAScript只支持實現(xiàn)繼承,而且其實現(xiàn)繼承主要是依靠原型鏈來實現(xiàn),下文給大家技術js實現(xiàn)繼承的六種方式,需要的朋友參考下2016-03-03

