python运行execjs中出现编码问题

在破解豆瓣的window.__DATA__时,使用python的execjs执行js,会有这个错误:


Exception in thread Thread-1:
Traceback (most recent call last):
  File "E:\codes\python\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "E:\codes\python\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "E:\codes\python\lib\subprocess.py", line 1238, in _readerthread
    buffer.append(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 41: illegal multibyte sequence

Traceback (most recent call last):
  File "E:/Python/python_project/decrypt_js/douban.py", line 13, in <module>
    data = ctx.call('decrypt', r)
  File "E:\codes\python\lib\site-packages\execjs\_abstract_runtime_context.py", line 37, in call
    return self._call(name, *args)
  File "E:\codes\python\lib\site-packages\execjs\_external_runtime.py", line 92, in _call
    return self._eval("{identifier}.apply(this, {args})".format(identifier=identifier, args=args))
  File "E:\codes\python\lib\site-packages\execjs\_external_runtime.py", line 78, in _eval
    return self.exec_(code)
  File "E:\codes\python\lib\site-packages\execjs\_abstract_runtime_context.py", line 18, in exec_
    return self._exec_(source)
  File "E:\codes\python\lib\site-packages\execjs\_external_runtime.py", line 87, in _exec_
    output = self._exec_with_pipe(source)
  File "E:\codes\python\lib\site-packages\execjs\_external_runtime.py", line 103, in _exec_with_pipe
    stdoutdata, stderrdata = p.communicate(input=input)
  File "E:\codes\python\lib\subprocess.py", line 939, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "E:\codes\python\lib\subprocess.py", line 1288, in _communicate
    stdout = stdout[0]
IndexError: list index out of range

原因是有一个程序在使用TextIOWrapper 类创建对象时默认使用了cp936的编码,也就是gbk编码,读取不了utf-8的字符,
所以我们可以修改下 subprocess.py 文件的默认编码方式为utf-8即可

在代码行656有个初始化,直接修改默认即可,如下
image.png

修改完再运行代码,结果就出来了,没有报错

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值