Python爬虫报错raise BadStatusLine(line) http.client.BadStatusLine: <html lang=“en“>

Python爬虫报错raise BadStatusLine line

  1. 程序:
import urllib.request

url = 'https://www.baidu.com'

headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
# 请求对象的定制
request = urllib.request.Request(url=url, headers=headers)
# urlopen()的参数url可以传递字符串或Request对象
response = urllib.request.urlopen(url=request)

content = response.read().decode('utf-8')

print(content)
  1. 报错:
Traceback (most recent call last):
  File "D:/Python爬虫/MyProject爬虫/12_爬虫_urllib_异常.py", line 15, in <module>
    response = urllib.request.urlopen(request)
  File "D:\python\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "D:\python\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "D:\python\lib\urllib\request.py", line 640, in http_response
    response = self.parent.error(
  File "D:\python\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "D:\python\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
  File "D:\python\lib\urllib\request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "D:\python\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "D:\python\lib\urllib\request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "D:\python\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
  File "D:\python\lib\urllib\request.py", line 1397, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "D:\python\lib\urllib\request.py", line 1358, in do_open
    r = h.getresponse()
  File "D:\python\lib\http\client.py", line 1347, in getresponse
    response.begin()
  File "D:\python\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "D:\python\lib\http\client.py", line 289, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: <html lang="en">

  1. 解决方法:换一个UA
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0'
}
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

sheygshsi

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值