[英]Why is python requests throwing this BadStatusLine exception
In python if I import requests and do:在 python 中,如果我导入请求并执行:
t = requests.get("http://www.azlyrics.com/u/urban.html")
I get this exception:我得到这个例外:
raise BadStatusLine(line)
http.client.BadStatusLine: ''
Does anyone know how to fix this?有谁知道如何解决这一问题?
There can be different reasons for this kind of error , but in this particular case this looks like a simple web-scraping detection - it can be solved by providing a User-Agent header pretending to be a real browser:这种错误可能有不同的原因,但在这种特殊情况下,这看起来像一个简单的网络抓取检测- 可以通过提供一个伪装成真实浏览器的User-Agent 标头来解决:
In [2]: requests.get("http://www.azlyrics.com/u/urban.html")
...
ConnectionError: ('Connection aborted.', BadStatusLine("''",))
In [3]: requests.get("http://www.azlyrics.com/u/urban.html", headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'})
Out[3]: <Response [200]>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.