Python爬虫headers处理及网络超时问题解决方案

作者:夏日的向日葵 时间:2022-11-19 23:38:34 

我们有时请求服务器时,无论get或post请求,会出现403错误,这是因为服务器拒绝了你的访问,这时我们可以通过模拟浏览器的头部信息进行访问,这样就可以解决反爬设置的问题。


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

来源:https://www.cnblogs.com/xiao02fang/p/12927267.html

标签:Python,爬虫,headers,网络
0
投稿

猜你喜欢

  • Pygame游戏开发之太空射击实战入门篇

    2023-07-17 23:12:42
  • Sql Server中常用的6个自定义函数分享

    2024-01-17 05:05:40
  • 浅析node命令行交互原理

    2024-05-11 10:14:36
  • python使用python-pptx删除ppt某页实例

    2022-10-23 16:19:59
  • Python爬虫与反爬虫大战

    2023-06-05 04:11:54
  • scrapy实践之翻页爬取的实现

    2023-09-25 23:09:26
  • python用tkinter实现一个gui的翻译工具

    2022-12-13 11:30:48
  • Oracle 游标使用总结

    2009-10-02 17:36:00
  • Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】

    2023-12-10 15:45:59
  • Python实现自动驾驶训练模型

    2023-07-28 18:45:29
  • mysql myisam 优化设置设置

    2024-01-15 11:04:52
  • 10种Java开发者编写SQL语句时常见错误

    2024-01-16 17:44:26
  • mybatis统计每条SQL的执行时间的方法示例

    2024-01-28 12:54:43
  • Python BS4库的安装与使用详解

    2021-06-30 02:00:22
  • 对django中foreignkey的简单使用详解

    2021-09-04 21:04:20
  • BERT vs GPT自然语言处理中的关键差异详解

    2022-04-01 08:15:36
  • Python实现Logger打印功能的方法详解

    2023-04-16 23:02:43
  • mysql如何通过当前排序字段获取相邻数据项

    2024-01-13 02:24:25
  • mysql left join快速转inner join的过程

    2024-01-26 19:08:17
  • 详解duck typing鸭子类型程序设计与Python的实现示例

    2022-12-26 07:12:19
  • asp之家 网络编程 m.aspxhome.com