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'
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值