Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
来源:互联网 发布:8月外贸数据 编辑:程序博客网 时间:2024/06/05 06:23
1. 问题的提出
在代码中,忽然碰到了如下错误:
'2017-04-25 13:15:13 PM' HttpUtil.py[line:30] ERROR HttpUtil:Header value 1 must be of type str or bytes, not <class 'int'>使用的代码如下:
response = requests.get(url, headers=headers, cookies=cookies)主体的代码是使用requests进行Http的页面调用访问。
引入import traceback来打印错误信息, 在异常捕获部分添加traceback的打印语句:
traceback.print_exc()其将详细的异常栈打印到控制台上去。
Traceback (most recent call last): File "/usr/local/python3/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap self.run() File "/usr/local/python3/lib/python3.6/multiprocessing/process.py", line 93, in run self._target(*self._args, **self._kwargs) File "/opt/crawler/web-crawler/dyproxy/IPProxyPool/spider/ProxyCrawl.py", line 34, in startProxyCrawl crawl.run() File "/opt/crawler/web-crawler/dyproxy/IPProxyPool/spider/ProxyCrawl.py", line 49, in run parserList = self.urltaker.parse_list() File "/opt/crawler/web-crawler/dyproxy/IPProxyPool/spider/urls/URLRetriever.py", line 35, in parse_list site_config['urls'] = getattr(self, 'retrieve_' + site.xpath("@name")[0])(site.xpath("@url")[0], site.xpath('./setting/@url')[0]) File "/opt/crawler/web-crawler/dyproxy/IPProxyPool/spider/urls/URLRetriever.py", line 110, in retrieve_mimiip response = HttpUtil.get(site_url % proxy, headers=all_headers['mimiip']) File "/opt/crawler/web-crawler/dyproxy/IPProxyPool/util/HttpUtil.py", line 31, in get traceback.print_exc() File "/usr/local/python3/lib/python3.6/traceback.py", line 159, in print_exc print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain) File "/usr/local/python3/lib/python3.6/traceback.py", line 101, in print_exception print(line, file=file, end="")基本确定就是headers或者cookies的问题。
2. 问题分析
基于错误信息,感觉是headers中的数据类型问题,期望string,但是实际上是int类型。经过检查发现是在自定义的header中:
"mimiip": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding":"gzip, deflate", "Accept-Language":"zh-CN,zh;q=0.8,de;q=0.6,en;q=0.4,zh-TW;q=0.2", "Cache-Control":"Max-Age=0", "Connection":"keep-alive", "DNT":1, "Host":"www.mimiip.com", "Referer":"http://www.mimiip.com/gngao/", "If-None-Match":"169bceb52ed4a052e3b0f82bda5b0462", "Upgrade-Insecure-Requests":1, "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3063.4 Safari/537.36" }其中使用了两个数字类型的,估计应该是这个位置的问题。
3. 问题解决
将上述中的int类型的数据,修改为string类型,则可以正常访问。
4. 总结
在Http请求中,所有的都是字符串,没有数字类型的,尤其是在构建的请求中,请注意这个问题。
0 0
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- python提示错误TypeError: write() argument must be str, not bytes
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- Python中报错TypeError: must be str, not bytes
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- TypeError: must be str, not bytes
- write() argument must be str, not bytes
- TypeError: must be str, not bytes
- Python3.x执行POST请求是出现POST data should be bytes or an iterable of bytes. It cannot be of type str.错误
- XCode中互相包含引起的"property with 'retain(or strong)' attribute must be of object type"错误与解决
- can't use a string pattern on a bytes-like object错误和must be str, not bytes错误
- 保存网页TypeError: must be str, not bytes
- TypeError: the JSON object must be str, not 'bytes'
- TypeError: write() argument must be str, not bytes
- docx2txt TypeError: write() argument must be str, not bytes
- 关于TypeError: strptime() argument 1 must be str, not bytes解析
- drools入门(二)-----规则引擎Drools解决汽水问题(复杂逻辑)
- hive上创建表视图
- 二叉树的遍历
- kafka详细介绍
- Android AudioSystem模块分析
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- encodeURIComponent编码后java后台的解码
- Java中关于空格遇到的一些问题
- jquery在JSP中无效的原因
- 图的邻接表存储下的深度优先遍历
- bzoj3133 [Baltic2013]ballmachine
- 把Tomcat的http改为https的步骤、方法
- 泛型类,泛型方法,dynamic, default
- wordpress点击文章找不到网页