Python爬虫报错raise BadStatusLine line
- 程序:
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)
response = urllib.request.urlopen(url=request)
content = response.read().decode('utf-8')
print(content)
- 报错:
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">
解决方法:换一个UA
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0'
}