解决Python3.6,发送POST请求错误提示:POST data should be bytes, an iterable of bytes, or a file object. It canno
2017-12-12 00:08
1031 查看
之前遇到的坑,今天又遇到了,在此记录一下,
爬虫时,转码data时,出现下面错误信息:
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
测试代码:
报错截图:
查看源文档:https://docs.python.org/3/library/urllib.parse.html
简洁明了一句话,转码为bytes类型
修改代码如下:
# -*- coding:utf-8 -*-
import urllib.request
def movieSpider():
"""
模拟Ajax请求
"""
url = "https://movie.douban.com/j/chart/top_list?"
header = {"User-Agent" : "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.7.62 Version/11.01"}
formData = {
"type" : "11",
"interval_id" : "100:90",
"action" : "",
"start" : "0",
"limit" : "20"
}
#将str类型转换为bytes类型
data = urllib.parse.urlencode(formData).encode("utf-8")
request = urllib.request.Request(url, data=data, headers=header)
print(urllib.request.urlopen(request).read().decode("utf-8"))
if __name__ == "__main__":
movieSpider()
测试结果(部分截图):
总结:
多看源文档少踩坑
爬虫时,转码data时,出现下面错误信息:
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 def movieSpider(): """ 模拟Ajax请求 """ url = "https://movie.douban.com/j/chart/top_list?" header = {"User-Agent" : "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.7.62 Version/11.01"} formData = { "type" : "11", "interval_id" : "100:90", "action" : "", "start" : "0", "limit" : "20" } data = urllib.parse.urlencode(formData) request = urllib.request.Request(url, data=data, headers=header) print(url) print(data) print(urllib.request.urlopen(request).read()) if __name__ == "__main__": movieSpider()
报错截图:
查看源文档:https://docs.python.org/3/library/urllib.parse.html
Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. If the resultant string is to be used as a data for POST operation with the urlopen() function, then it should be encoded to bytes, otherwise it would result in a TypeError. |
修改代码如下:
# -*- coding:utf-8 -*-
import urllib.request
def movieSpider():
"""
模拟Ajax请求
"""
url = "https://movie.douban.com/j/chart/top_list?"
header = {"User-Agent" : "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.7.62 Version/11.01"}
formData = {
"type" : "11",
"interval_id" : "100:90",
"action" : "",
"start" : "0",
"limit" : "20"
}
#将str类型转换为bytes类型
data = urllib.parse.urlencode(formData).encode("utf-8")
request = urllib.request.Request(url, data=data, headers=header)
print(urllib.request.urlopen(request).read().decode("utf-8"))
if __name__ == "__main__":
movieSpider()
测试结果(部分截图):
总结:
多看源文档少踩坑
相关文章推荐
- python3 爬虫TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be
- TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type s
- python3.x执行post请求时报错“POST data should be bytes or an iterable of bytes...”的解决方法
- python3 “POST data should be bytes or an iterable of bytes...”的解决方法
- python2.7web代码用python3.6运行时,浏览器发送请求报错TypeError: Object of type 'bytes' is not JSON serializable的解决办法
- python3爬虫问题 POST data should be bytes or an iterable of bytes
- python3爬虫问题 POST data should be bytes or an iterable of bytes
- VS2010 出现错误提示“Error Creating Control - Object reference not set to an instance of an objec” 的解决方法
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- 配置caffe的python环境时make pycaffe提示fatal error: numpy/arrayobject.h No such file or directory解决方法
- 错误处理:"Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was
- Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- silverlight安装后仍然提示安装以及打开vs的xaml页面提示Object reference not set to an instance of an object 错误
- Ionic/Angular TypeScript 错误:Object prototype may only be an Object or null: undefined的解决办法
- 〖Android〗arm-linux-androideabi-gdb报 libpython2.6.so.1.0: cannot open shared object file错误的解决方法
- Gerrit 执行 git pull 的时候,提示 fatal: Out of memory, malloc failed (tried to allocate *** bytes)错误的解决方法
- Nodejs发送Post请求时出现socket hang up错误的解决办法
- webview使用遇到 It is possible that this object was over-released, or is in the process of deallocation错误的解决办法
- Linux运行脚本命令提示No such file or directory错误的解决办法