gpt4 book ai didi

python mechanize文件上传UnicodeDecode错误

转载 作者:行者123 更新时间:2023-12-01 05:38:12 25 4
gpt4 key购买 nike

所以我有一个小脚本,我想用它来将一些 PDF 上传到我选择的引文站点 (citeulike.org)

问题是它不起作用。它的作用是:

so want to upload /Users/willwade/Dropbox/Papers/price_promoting_643127.pdf  to  12589610
Traceback (most recent call last):
File "citeuupload.py", line 167, in <module>
cureader.parseUserBibTex()
File "citeuupload.py", line 160, in parseUserBibTex
self.uploadFileToCitation(b['citeulike-article-id'],self.localpapers+fileorfalse)
File "citeuupload.py", line 138, in uploadFileToCitation
resp = self.browser.submit()
File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 541, in submit
File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 203, in open
File "build/bdist.macosx-10.8-intel/egg/mechanize/_mechanize.py", line 230, in _mech_open
File "build/bdist.macosx-10.8-intel/egg/mechanize/_opener.py", line 193, in open
File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 344, in _open
File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 1142, in http_open
File "build/bdist.macosx-10.8-intel/egg/mechanize/_urllib2_fork.py", line 1115, in do_open
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 955, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 989, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 951, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 809, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 544: ordinal not in range(128)

和代码:

def uploadFileToCitation(self,artid,file):
print 'so want to upload', file, ' to ', artid
self.browser.open('http://www.citeulike.org/user/'+cUser+'/article/'+artid)
self.browser.select_form(name="fileupload_frm")
self.browser.form.add_file(open(file, 'rb'), 'application/pdf', file, name='file')
try:
resp = self.browser.submit()
self.wait_for_api_limit()
except mechanize.HTTPError, e:
print 'error'
print e.getcode()
print resp.read()
exit()

注意:我可以看到它正在正确读取文件(并且它确实存在)。另请注意,我在其他地方这样做

    self.browser = mechanize.Browser()
self.browser.set_handle_robots(False)
self.browser.addheaders = [
("User-agent", 'me@me.com citeusyncpy/1.0'),
]

Full code is here

最佳答案

尝试检查这个similar question

为了澄清,该消息是在 httplib 中根据方法、URL、 header 等构造的。如果其中任何一个是 Unicode,则整个字符串将转换为 Unicode(我认为这是正常的 Python 行为) )。然后,如果您尝试附加 UTF-8 字符串,您会收到我在原始问题中描述的错误...

从外观上看,这是编码问题,正确的 header 可以修复。

您也可以查看这个issue .

关于python mechanize文件上传UnicodeDecode错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18346966/

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