解决Python3.6,发送POST请求错误提示:POST data should be bytes, an iterable of bytes, or a file object. It canno
来源:互联网 发布:淘宝双十一数据统计 编辑:程序博客网 时间:2024/06/05 10:46
之前遇到的坑,今天又遇到了,在此记录一下,
爬虫时,转码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.requestdef 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.简洁明了一句话,转码为bytes类型修改代码如下:
# -*- coding:utf-8 -*-import urllib.requestdef 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()
总结:
多看源文档少踩坑
阅读全文
0 0
- 解决Python3.6,发送POST请求错误提示:POST data should be bytes, an iterable of bytes, or a file object. It canno
- 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. It cannot be of type str.错误
- python3.x执行post请求时报错“POST data should be bytes or an iterable of bytes...”的解决方法
- python3爬虫问题 POST data should be bytes or an iterable of bytes
- python3爬虫问题 POST data should be bytes or an iterable of bytes
- 解决python3的pickle.load错误:a bytes-like object is required, not 'str'
- can't use a string pattern on a bytes-like object错误和must be str, not bytes错误
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- php curl_exec post data more than 1K bytes
- python3 TypeError: a bytes-like object is required, not 'str'
- python3 TypeError: a bytes-like object is required, not 'str'
- 正则匹配href错误 expected string or bytes-like object
- Java - Encodes a String Object into a Sequence of bytes
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- unpack requires a bytes object of length 4
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- A标签发送post请求
- Python基础-装饰器-Decorator
- Scala闭包
- error LNK 2019:VS+MFC+双摄像头的打开
- 算法(7)最大值和最小值
- 2017年冬季浙大PAT真题(附本人代码)
- 解决Python3.6,发送POST请求错误提示:POST data should be bytes, an iterable of bytes, or a file object. It canno
- 动态内存函数:malloc(n字节),calloc(),realloc()
- 多进程二
- 从输入网址到页面呈现都发生了什么?
- Xdebug + Eclipse 单步跟踪调试PHP代码
- 02-vueJs开发环境搭建
- vue项目用js封装的md5
- 关于SSM与echart结合的问题总结
- RIOT物联网操作系统