在Python3.x中,运行程序,提示错误:TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
#coding : utf-8 import urllib.request import http.cookiejar url = 'http://www.baidu.com' print('第一种方法') #直接请求 response1 = urllib.request.urlopen(url) #返回状态码,如果是200,表示获取成功 print(response1.getcode()) #读取内容 cont = response1.read() print('第二种方法') request = urllib.request.Request(url) #添加数据 request.data = 'a' #添加http的header request.add_header('User-Agent', 'Mozilla/5.0') #发送请求获取结果 response2 = urllib.request.urlopen(request) print(response2.getcode()) print('第三种方法') #创建cookie容器 cj = http.cookiejar.CookieJar() #创建一个opener opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) #给url安装opener urllib.request.install_opener(opener) #使用带有cookie的urllib访问网页 response3 = urllib.request.urlopen(url) print(response3.getcode())
请问各位大神,这是哪里出问题了