报错:TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
出现找个问题的原因是:我们需要在urllib.parse.urlencode({"username":"xxxxx"})的时候需要字符转码;
解决:urllib.parse.urlencode({"username":"xxxx"}).encode(encode='UTF8')
代码如下:
##post请求
import urllib.request
values = {"username" : "xxxx" , "password" : "xxxx"}
data = urllib.parse.urlencode(values).encode(encoding='UTF8')
url = "https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn"
response = urllib.request.urlopen(url,data)
print(response.read())
##get请求
import urllib.request
values ={}
values["username"] = "xxxx"
values["password"] = "xxxx"
data = urllib.parse.urlencode(values)
url = "https://passport.csdn.net/account/login"
geturl = url+"?"+data
#geturl = url+"?username=xxxx&password=xxxx"
response = urllib.request.urlopen(geturl)
print(response.read())