Python之Headers value 1 must be of type str or bytes, not 錯誤的解決
阿新 • • 發佈:2019-01-10
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()
其將詳細的異常棧列印到控制檯上去。基本確定就是headers或者cookies的問題。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="")
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請求中,所有的都是字串,沒有數字型別的,尤其是在構建的請求中,請注意這個問題。