yfeng 发表于 2015-9-25 23:13:44

报错TypeError: POST data should be bytes or an iterable of bytes. It cannot b...

#! /usr/bin/env python3

import 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.

yfeng 发表于 2015-9-26 15:59:53

没人知道吗?

bbbbbb 发表于 2015-10-12 18:33:07

data的格式不能为str 要转码一下 直接把英语翻过来就行啦

微壤 发表于 2016-10-15 22:27:06

我遇到了和楼主一样的问题,查找后发现,应该改 data = urllib.parse.urlencode(values) 为: data = urllib.parse.urlencode(values).encode(encoding='utf-8')即可运行

bravsheng 发表于 2021-8-25 09:54:47

嗯,这确实是一个细节,这里的date要编码成utf8形式。    POST数据应该是字节或文件对象
页: [1]
查看完整版本: 报错TypeError: POST data should be bytes or an iterable of bytes. It cannot b...