相关文章推荐
才高八斗的骆驼  ·  ImportError: ...·  1 周前    · 
一身肌肉的拖把  ·  如何在 TypeScript ...·  1 年前    · 
怕考试的木耳  ·  Java 方法 | 菜鸟教程·  1 年前    · 
旅行中的长颈鹿  ·  javascript - How ...·  1 年前    · 
Traceback (most recent call last):
File "/home/appleyuchi/coai/src/merge/xiami_request/search_songs_request.py", line 32, in <module>
result=search_songs_request(searchword)
File "/home/appleyuchi/coai/src/merge/xiami_request/search_songs_request.py", line 26, in search_songs_request
res_data = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1201, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1136, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 453, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 417, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "/home/appleyuchi/coai/src/merge/xiami_request/search_songs_request.py"]
[dir: /home/appleyuchi/coai/src/merge/xiami_request]

[path: /home/appleyuchi/bin:/home/appleyuchi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

错误是不定几率出现的,有时候出现,有时候不出现,一开始我有点慌了。

解决方案如下:

根据下面参考链接来获取User Agent

http://blog.csdn.net/pop_rain/article/details/73178223


根据 http://www.useragentstring.com/
获得user-agent以后,在python代码中补充headers,填入在这个网站查到的user-agent的“值”即可,headers在python中是字典类型,也就是所谓的“键-值”对

“键”是User-Agent,

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

然后,代码由

req = urllib2.Request(url)

res_data = urllib2.urlopen(req)

res = res_data.read()

headers = { 'User-Agent':' Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 ' }
req = urllib2.Request(url,"",headers)
res_data = urllib2.urlopen(req)

res = res_data.read()

Traceback (most recent call last):  File "/home/appleyuchi/coai/src/merge/xiami_request/search_songs_request.py", line 32, in     result=search_songs_request(searchword)  File "/home/appleyuchi/
我是因为频繁的更换网络,导致这样的。 然后我回忆了一下。之前每次改ip都会把Package.json 中的 –host 0.0.0.0改成我的固定ip,由于改来改去太麻烦了,我就设置为0.0.0.0 所以喜提上图的一排get请求 “dev”: “webpack-dev-server –inline –progress –config build/webpack.dev.conf.js –host 0.0.0.0”, 把 –host 0.0.0.0 改成你的固定ip 补充知识:Vue不能检测数组下标赋值、长度变化、对象属性动态添加和删除的原因和解决办法 解决方法: 1、方案一
BadStatusLine 错误     使用request的get时 出现 BadStatusLine 错误,初步确定是因为参数过长引起的; 但是换了post以后还是出错。 曲折的最终发现是因为使用post的参数params是 给url中指定参数使用的,要将参数放到body中,post需要使用参数data指定。 response = resquests.post(url, data = dat
http://www.useragentstring.com/ https://cuiqingcai.com/990.html https://blog.csdn.net/adonis_lu37/article/details/79225947?utm_source=blogxgwz7 from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 1) profile.set_preference('network.proxy.http', 'proxy_url') profile. raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine (" '' ", )) data = {"host":host,"key":key,"value":value,"dns":ip} res = requests.post(url=url,json = data) 方法1: 加重试
Python 爬虫报错requests.exceptions.ConnectionError: (‘Connection aborted.‘, BadStatusLine (“‘‘“,))
Python 爬虫报错requests.exceptions.ConnectionError: (‘Connection aborted.’, BadStatusLine ("’’",)) Python 爬虫报错requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine (" '' ",)) 这个报错的原因一般由于网络状...