Python爬虫headers处理及网络超时问题解决方案
本文详解Python爬虫中常见的403禁止访问及网络超时问题。通过模拟浏览器User-Agent解决反爬限制,利用requests模块设置timeout参数处理超时,并演示如何捕获ReadTimeout、HTTPError等具体异常类,提升爬虫稳定性。
解决403禁止访问:模拟浏览器Headers
1、请求headers处理
在进行GET或POST请求时,若服务器返回403状态码,通常意味着访问被拒绝。此时可通过在请求头中模拟浏览器的User-Agent信息来绕过反爬机制,从而成功获取数据。
import requests
# 创建需要爬取网页的地址
url = 'https://www.baidu.com/'
# 创建头部信息
headers = {'User-Agent':'OW64; rv:59.0) Gecko/20100101 Firefox/59.0'}
# 发送网络请求
response = requests.get(url, headers=headers)
# 以字节流形式打印网页源码
print(response.content)
运行结果如下:
b'<!DOCTYPE html><!--STATUS OK-->\n\n\n \n \n <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><meta name="description" content="\xe5\x85\xa8\xe7\x90\x83\xe6\x9c\x80\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe6\x90\x9c\xe7\xb4\xa2\xe5\xbc\x95\xe6\x93\x8e\xe3\x80\x81\xe8\x87\xb4\xe5\x8a\x9b\xe4\xba\x8e\xe8\xae\xa9\xe7\xbd\x91\xe6\xb0\x91\xe6\x9b\xb4\xe4\xbe\xbf\xe6\x8d\xb7\xe5\x9c\xb0\xe8\x8e\xb7\xe5\x8f\x96\xe4\xbf\xa1\xe6\x81\xaf\xef\xbc\x8c\xe6\x89\xbe\xe5\x88\xb0\xe6\x89\x80\xe6\xb1\x82\xe3\x80\x82\xe7\x99\xbe\xe5\xba\xa6\xe8\xb6\x85\xe8\xbf\x87\xe5\x8d\x83\xe4\xba\xbf\xe7\x9a\x84\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\x91\xe9\xa1\xb5\xe6\x95\xb0\xe6\x8d\xae\xe5\xba\x93\xef\xbc\x8c\xe5\x8f\xaf\xe4\xbb\xa5\xe7\x9e\xac\xe9\x97\xb4\xe6\x89\xbe\xe5\x88\xb0\xe7\x9b\xb8\xe5\x85\xb3\xe7\x9a\x84\xe6\x90\x9c\xe7\xb4\xa2\xe7\xbb\x93\xe6\x9e\x9c\xe3\x80\x82"><link rel="shortcut icon" href="/favicon.ico" rel="external nofollow" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" rel="external nofollow" title="\xe7\x99\xbe\xe5\xba\xa6\xe6\x90\x9c\xe7\xb4\xa2" /><link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg" rel="external nofollow" ><link rel="dns-prefetch" href="//dss0.bdstatic.com" rel="external nofollow" /><link rel="dns-prefetch" href="//dss1.bdstatic.com" rel="external nofollow" /><link rel="dns-prefetch" href="//ss1.bdstatic.com" rel="external nofollow" /><link rel="dns-prefetch" href="//sp0.baidu.com" rel="external nofollow" /><link rel="dns-prefetch" href="//sp1.baidu.com" rel="external nofollow" /><link rel="dns-prefetch" href="//sp2.baidu.com" rel="external nofollow" />
处理网络超时与异常捕获
2、网络超时问题
当目标网页响应时间过长时,系统会判定为超时并中断连接。以下代码演示了如何模拟网络超时的场景。
import requests
# 循环发送请求50次
for a in range(1, 50):
# 捕获异常
try:
# 设置超时为0.5秒
response = requests.get('https://www.baidu.com/', timeout=0.5)
# 打印状态码
print(response.status_code)
# 捕获异常
except Exception as e:
# 打印异常信息
print('异常'+str(e))
运行结果如下:
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
上述代码执行了50次循环请求,并将超时时间设置为0.5秒。若服务器在0.5秒内未响应,程序将捕获超时信息并打印至控制台。
针对网络异常,requests模块提供了三种常见的异常类,具体示例代码如下:
import requests
# 导入requests.exceptions模块中的三种异常类
from requests.exceptions import ReadTimeout,HTTPError,RequestException
# 循环发送请求50次
for a in range(1, 50):
# 捕获异常
try:
# 设置超时为0.5秒
response = requests.get('https://www.baidu.com/', timeout=0.5)
# 打印状态码
print(response.status_code)
# 超时异常
except ReadTimeout:
print('timeout')
# HTTP异常
except HTTPError:
print('httperror')
# 请求异常
except RequestException:
print('reqerror')
运行结果如下:
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
200
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









