Python Mechanize:上传 UTF-8 文件时出现 UnicodeEncodeError。 “ascii”编解码器

发布于 2024-10-13 07:16:12 字数 1297 浏览 3 评论 0原文

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 文件的上传吗?

the mechanize framework works great for automating the first couple of web screens. The problem is where it needs to upload a file with in a form.

Here is the section of code just before the error:

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")

It results in the following error at runtime:

    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)

Any idea how to make mechanize handle upload of a UTF-8 file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-10-20 07:16:13

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

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

Mechanize seems to expect the file data as raw bytes, not Unicode data. Try opening the file using the usual open() function:

...
xmlFile = open("MyFile.xml", "rt")
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文