def indetna(self): content = open('baidujs.js', 'r', encoding='utf-8').read() data_js = execjs.compile(content) name = input(str('请输入需要翻译的文字:')) sign = data_js.call("cici", name) data = { "from": "zh", "to": "en", "query": name, "transtype": "realtime", "simple_means_flag": "3", "sign": sign, "token": "8ec1373cf602205f5c09038491a04a8b", "domain": "common", "ts": "1688730628449" } return data def dmet(self, datan): resqone = requests.post(url=self.url, data=datan, headers=self.headers) dict_data = json.loads(resqone.text)["trans_result"]["data"][0]['dst'] print(dict_data) def main(self): korr = self.indetna() self.dmet(korr)
时间: 2023-07-21 09:00:02 浏览: 143
根据您提供的代码,您已经更新了`indetna()`方法,将签名生成的逻辑移动到了JavaScript文件中。现在您只需要调用`indetna()`方法并传递正确的参数即可。
请确保您已经创建了名为`baidujs.js`的JavaScript文件,并在其中实现了一个名为`cici()`的函数,它接受一个参数并返回一个签名。
如果您仍然遇到问题,请提供完整的错误信息以便我能够更好地帮助您。
相关问题
Traceback (most recent call last): File "D:\pythonFiles\wikiextractor-master\wikiextractor-master\wikiextractor\WikiExtractor.py", line 648, in <module> main() File "D:\pythonFiles\wikiextractor-master\wikiextractor-master\wikiextractor\WikiExtractor.py", line 642, in main process_dump(input_file, args.templates, output_path, file_size, File "D:\pythonFiles\wikiextractor-master\wikiextractor-master\wikiextractor\WikiExtractor.py", line 428, in process_dump reduce.start() File "D:\软件\python\lib\multiprocessing\process.py", line 121, in start self._popen = self._Popen(self) File "D:\软件\python\lib\multiprocessing\context.py", line 327, in _Popen return Popen(process_obj) File "D:\软件\python\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__ reduction.dump(process_obj, to_child) File "D:\软件\python\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) TypeError: cannot pickle '_io.TextIOWrapper' object (venv) D:\pythonFiles\wikiextractor-master\wikiextractor-master>Traceback (most recent call last): File "<string>", line 1, in <module> File "D:\软件\python\lib\multiprocessing\spawn.py", line 102, in spawn_main source_process = _winapi.OpenProcess( OSError: [WinError 87] 参数错误。 怎么解决
这个错误通常是由于在使用pickle模块将对象序列化时,对象中存在不能被序列化的属性或方法所导致的。有可能是在使用多进程库时,进程间传递了一个不能被序列化的_io.TextIOWrapper对象,导致了这个错误。你可以尝试将输出路径参数output_path改为一个字符串类型而非一个文件对象类型。另外,你可以尝试将输出文件的写入操作放在主进程中,而非在子进程中进行。这样可以避免在子进程中对文件对象进行pickle序列化。以下是一个可能的解决方案:
``` python
def worker_process(input_queue, output_queue, output_path):
# create output file in main process
with open(output_path, 'w', encoding='utf-8') as output_file:
while True:
data = input_queue.get()
if data is None:
break
# process data
# ...
output = process_data(data)
# put result into output queue
output_queue.put(output)
# write result to output file
output_file.write(output)
def process_dump(input_file, templates, output_path, file_size, process_count):
# create input queue
input_queue = Queue(maxsize=process_count)
# create output queue
output_queue = Queue(maxsize=process_count)
# spawn processes
processes = []
for i in range(process_count):
p = Process(target=worker_process, args=(input_queue, output_queue, output_path))
processes.append(p)
p.start()
# read input file and put data into input queue
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
input_queue.put(line)
# stop worker processes
for i in range(process_count):
input_queue.put(None)
for p in processes:
p.join()
# collect results from output queue and write to output file
with open(output_path, 'w', encoding='utf-8') as output_file:
while not output_queue.empty():
output = output_queue.get()
output_file.write(output)
```
在这个改进的代码中,我们将输出路径参数output_path改为一个字符串类型,并在主进程中打开输出文件,将输出文件的写入操作放在主进程中进行,避免在子进程中对文件对象进行pickle序列化。
import os yaml_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'data', 'GuanWang.yaml') ini_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath('__file__'))), 'data', 'GuanWang.ini') print(ini_path) class FileRead: def __init__(self): self.yaml_path = yaml_path self.ini_path = ini_path def read_yaml(self): f = open(self.yaml_path, encoding='utf-8')代码解释
1. The code imports the 'os' module.
2. The code sets the 'yaml_path' variable to the path of the 'GuanWang.yaml' file.
3. The code sets the 'ini_path' variable to the path of the 'GuanWang.ini' file.
4. The 'print' statement is used to print the 'ini_path' variable to the console.
5. The 'FileRead' class is defined.
6. The 'self.yaml_path' and 'self.ini_path' instance variables are initialized to the 'yaml_path' and 'ini_path' variables, respectively.
7. The 'read_yaml' method is defined.
8. The 'open' function is used to open the 'yaml_path' file in read mode with the 'utf-8' encoding.
9. The opened file object is returned.
阅读全文