python3错误:无法连接str到字节

时间:2018-07-05 02:07:51

标签: python python-3.x urllib

我想使用python登录网站,并向/login发出发布请求,并且我想查看http发布结果,但是它显示了一个错误:无法将str连接为字节,这是什么?这个错误是什么意思?我该如何解决呢?我的python版本是3.6.5。

#! python3
# https://shimo.im/login
import urllib.request
import ssl

loginUrl = 'https://shimo.im/login'
context = ssl._create_unverified_context()
headers = {'content-type': "application/x-www-form-urlencode"}
payload = {
    'email': 'aaa@qq.com',
    'password': 'dddddd+',
    'remember': 'check'
}

req = urllib.request.Request(loginUrl, payload, headers=headers)
data = urllib.request.urlopen(req, context=context).read()

print(data)

更详细的错误信息:

Traceback (most recent call last):
  File "E:/test/python-test/learn.py", line 17, in <module>
    data = urllib.request.urlopen(req, context=context).read()
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\dev2\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1064, in _send_output
    + b'\r\n'
TypeError: can't concat str to bytes

0 个答案:

没有答案