为什么python请求抛出这个BadStatusLine异常 [英] Why is python requests throwing this BadStatusLine exception

查看:29
本文介绍了为什么python请求抛出这个BadStatusLine异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 中,如果我导入请求并执行:

In python if I import requests and do:

t = requests.get("http://www.azlyrics.com/u/urban.html")

我收到此异常:

raise BadStatusLine(line)
http.client.BadStatusLine: ''

有人知道如何解决这个问题吗?

Does anyone know how to fix this?

推荐答案

可以有不同 原因 对于这种错误,但在这种特殊情况下,这看起来像一个简单的网络抓取检测 - 它可以通过提供一个 User-Agent 标头来伪装成一个真正的浏览器来解决:

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:

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]>

这篇关于为什么python请求抛出这个BadStatusLine异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆