使用Python 3和RoboBrowser向Facebook提交表单并获取UnicodeDecodeError

时间:2018-08-21 09:04:09

标签: python-3.x robobrowser

因此,我正在尝试制作一个脚本,该脚本将使用RoboBrowser为我自动将图像上传到Facebook,以导航mbasic.facebook.com网站,并且在提交图像表格时出现一个奇怪的错误:

Traceback (most recent call last):
  File "C:\Users\Admin\OneDrive\Facebook Project\facebook.py", line 55, in <module>
browser.submit_form(form, submit=form["add_photo_done"])
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\robobrowser\browser.py", line 343, in submit_form
response = self.session.request(method, url, **send_args)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 498, in request
prep = self.prepare_request(req)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 441, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 312, in prepare
self.prepare_body(data, files, json)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 500, in prepare_body
(body, content_type) = self._encode_files(files, data)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 159, in _encode_files
fdata = fp.read()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 23, in decode
  return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 57: character maps to <undefined>

为什么会这样?我猜这与我如何处理将图像文件提交到表单有关,但是我对为什么感到困惑。我的相关代码如下:

form = browser.get_forms()
form = form[0]
image = os.path.dirname(os.path.realpath(__file__)) + r"\test.png"
form['file1'] = image
browser.submit_form(form, submit=form["add_photo_done"])
print(browser.parsed())

编辑:
我不认为这是 UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to 的转载,因为我没有读取代码中的任何文件,提交表单时似乎出现了错误。无论如何,我已经看过这篇文章了,我无法解决如何使用它来解决我的问题。

1 个答案:

答案 0 :(得分:0)

弄清楚我需要发送一个图像对象,而不是图像的路径。下面的解决方案代码(如果有人在下面):

image = open(os.path.dirname(os.path.realpath(__file__)) + r"\test.jpg", 'rb')