gpt4 book ai didi

Python Mechanize : UnicodeEncodeError when uploading UTF-8 file. 'ascii'编解码器

转载 作者:行者123 更新时间:2023-11-30 23:55:18 25 4
gpt4 key购买 nike

Mechanize 框架非常适合自动化前几个网页屏幕。问题在于它需要在表单中上传文件。

这是错误之前的代码部分:

br.select_form(name="form.uploadXMLDataWizardForm")
xmlFile = codecs.open("MyFile.xml", "rt", "utf8")
br.form.add_file(file_object=xmlFile, content_type="text/xml", filename="MyFile.xml", name="dataFile")
br.submit(name="$action:next")

它会在运行时导致以下错误:

    br.submit(name="$action:next")
File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 541, in submit
File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 530, in click
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 2999, in click
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 3201, in _click
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 2350, in _click
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 3269, in _switch_click
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 3252, in _request_data
File "build/bdist.macosx-10.6-universal/egg/mechanize/_form.py", line 1341, in _write_mime_data
UnicodeEncodeError: 'ascii' codec can't encode characters in position 650-651: ordinal not in range(128)

知道如何让 mechanize 处理 UTF-8 文件的上传吗?

最佳答案

Mechanize 似乎期望文件数据为原始字节,而不是 Unicode 数据。尝试使用常用的 open() 函数打开文件:

...
xmlFile = open("MyFile.xml", "rt")
...

关于Python Mechanize : UnicodeEncodeError when uploading UTF-8 file. 'ascii'编解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com