报错TypeError: POST data should be bytes or an iterable of bytes. It cannot b...
#! /usr/bin/env python3import urllib.parse
import urllib.request
url = 'http://192.168.240.128/wp/wp-login.php'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'log' : 'xxx',
'pwd' : '123456'
}
data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data)
req.add_header('Referer', 'http://192.168.240.128/')
response = urllib.request.urlopen(req) ----> 为什么到这一步的时候老是报错呢:报错如下 :
the_page = response.read()
print(the_page.decode("utf8"))
报错:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
response = urllib.request.urlopen(req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 463, in open
req = meth(req)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1170, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str. 没人知道吗? data的格式不能为str 要转码一下 直接把英语翻过来就行啦 我遇到了和楼主一样的问题,查找后发现,应该改 data = urllib.parse.urlencode(values) 为: data = urllib.parse.urlencode(values).encode(encoding='utf-8')即可运行 嗯,这确实是一个细节,这里的date要编码成utf8形式。 POST数据应该是字节或文件对象
页:
[1]