当前位置: 首页 > news >正文

做起点说网站的服务器多少钱如何开发一个网站

做起点说网站的服务器多少钱,如何开发一个网站,dw做网站字体 别人 电脑,县城购物网站一、retrying模块简介 在爬虫中,因为我们是在线爬取内容,所以可能会因为网络、服务器等原因导致报错,那么这类错误出现以后,我们想要做的肯定是在报错处进行重试操作,Python提供了一个很好的模块,能够直接帮…

一、retrying模块简介

在爬虫中,因为我们是在线爬取内容,所以可能会因为网络、服务器等原因导致报错,那么这类错误出现以后,我们想要做的肯定是在报错处进行重试操作,Python提供了一个很好的模块,能够直接帮助我们实现重试操作,它就是retrying模块。当然,重试操作不仅仅只用于爬虫,还可以用于其他更广泛的领域。

retrying 是一个用Python编写的重试库,用于将重试行为添加到常规任务中,让你写的代码拥有重试功能。官方文档:https://github.com/rholder/retrying

二、案例讲解

我这里有一些网址,现在要求编写一个爬虫程序,将这些网站访问一遍,那么应该如何确保程序能够顺利的执行完成呢,有些小伙伴会说使用异常捕获防止程序出现意外,这是一种解决办法,那么如果我再要求一个网站的请求不能等待过长时间并且针对于有错误的地址进行重试,应该如何解决呢,这个时候我们就需要引入重试机制。

(1)无重试机制的爬虫

import requestsdef requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')UrlList = ['https://www.chinanews.com/scroll-news/news5.html', 'https://ssr4.scrape.center/','https://www.chinanews.com/scroll-news/news12.html', 'https://www.chinanews.com/scroll-news/news3.html','https://www.chinanews.com/scroll-news/news8.html', 'https://www.chinanews.com/scroll-news/news2.html','https://www.chinanews.com/scroll-news/news11.html', 'https://www.chinanews.com/scroll-news/news1.html','https://www.chinanews.com/scroll-news/news4.html', 'https://www.chinanews.com/scroll-news/news7.html','https://www.chinanews.com/scroll-news/news10.html', 'https://www.chinanews.com/scroll-news/news9.html','https://www.chinanews.com/scroll-news/news6.html']
for i in UrlList:print(f'正在访问:{i}')requests_url(i)

这段代码在执行时,报了Read timed out的异常提示超时,那么超时的原因有多种,可能是本地网络原因、也可能是网址服务器原因等,此时便需要使程序具备能够重试的功能。

(2)引入了重试机制的爬虫

接下来我们导入安装好的retrying模块,只要在需要重试的函数前面添加指定的装饰器,即可使程序具备重试功能,看以下修改:

from retrying import retry@retry
def requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')

(3)重试机制修订

引入了 retrying 的代码存在一个弊端,如果这个网址本身就是联不通的,那么它会永远重试下去,这并不是我们想看到的,所以还要添加一下参数:stop_max_attempt_number(最大重试次数),可以给这个参数指定一个数字,比如下面指定的数字5,便是让其最多重试5次。

from retrying import retry@retry(stop_max_attempt_number=5)
def requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')

但是这样的修改还是不尽人意,虽然添加了最大重试次数,但非常快速的机械性的连续重试,总有一种会随时出发反爬机制的感觉,所以还可以设置两次重试之间的等待时间:wait_fixed(单位是毫秒),代码如下:

from retrying import retry@retry(stop_max_attempt_number=5, wait_fixed=2000)
def requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')

不过我还是建议重复性的操作添加点随机性比较好,将 wait_fixed 修改为 wait_random_min 和 wait_random_max,表示从指定的时间范围内随机一个等待时间。

from retrying import retry@retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000)
def requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')

三、最终代码

import requests
from retrying import retry@retry(stop_max_attempt_number=5, wait_random_min=1000, wait_random_max=2000)
def requests_url(href):URL = hrefHeaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}resp = requests.get(url=URL, headers=Headers, timeout=3)if resp.status_code == 200:print(f'{href}访问成功')UrlList = ['https://www.chinanews.com/scroll-news/news5.html', 'https://ssr4.scrape.center/','https://www.chinanews.com/scroll-news/news12.html', 'https://www.chinanews.com/scroll-news/news3.html','https://www.chinanews.com/scroll-news/news8.html', 'https://www.chinanews.com/scroll-news/news2.html','https://www.chinanews.com/scroll-news/news11.html', 'https://www.chinanews.com/scroll-news/news1.html','https://www.chinanews.com/scroll-news/news4.html', 'https://www.chinanews.com/scroll-news/news7.html','https://www.chinanews.com/scroll-news/news10.html', 'https://www.chinanews.com/scroll-news/news9.html','https://www.chinanews.com/scroll-news/news6.html']
for i in UrlList:print(f'正在访问:{i}')try:requests_url(i)except:print('重试结束,依旧报错,跳过,进行下一项任务!')

四、总结

重试机制仅仅是让爬虫在某一网站访问不通的情况下多尝试几次,最终还是会报错,所以重试机制依旧需要与异常捕获结合使用。

文章转载自:
http://concubine.bbtn.cn
http://occasionalism.bbtn.cn
http://ghillie.bbtn.cn
http://oes.bbtn.cn
http://carpaccio.bbtn.cn
http://granulocytopenia.bbtn.cn
http://catface.bbtn.cn
http://dismay.bbtn.cn
http://braver.bbtn.cn
http://woodhouse.bbtn.cn
http://bannister.bbtn.cn
http://elution.bbtn.cn
http://practicing.bbtn.cn
http://cotswolds.bbtn.cn
http://stinginess.bbtn.cn
http://accidental.bbtn.cn
http://tepid.bbtn.cn
http://taxability.bbtn.cn
http://cottonweed.bbtn.cn
http://importer.bbtn.cn
http://eastern.bbtn.cn
http://disperse.bbtn.cn
http://oxisol.bbtn.cn
http://herry.bbtn.cn
http://pericarditis.bbtn.cn
http://pvt.bbtn.cn
http://bateleur.bbtn.cn
http://indecomposable.bbtn.cn
http://leisterer.bbtn.cn
http://febrifacient.bbtn.cn
http://foozle.bbtn.cn
http://prophylaxis.bbtn.cn
http://lawrencium.bbtn.cn
http://ornithoid.bbtn.cn
http://chorea.bbtn.cn
http://misdata.bbtn.cn
http://boise.bbtn.cn
http://glean.bbtn.cn
http://scan.bbtn.cn
http://tetraalkyllead.bbtn.cn
http://cholecystography.bbtn.cn
http://terebinth.bbtn.cn
http://scoresheet.bbtn.cn
http://fetus.bbtn.cn
http://actinospectacin.bbtn.cn
http://surfnet.bbtn.cn
http://circumplanetary.bbtn.cn
http://neosalvarsan.bbtn.cn
http://heterocaryotic.bbtn.cn
http://indices.bbtn.cn
http://lumpenproletarian.bbtn.cn
http://nameless.bbtn.cn
http://vermicelli.bbtn.cn
http://qoph.bbtn.cn
http://minesweeper.bbtn.cn
http://goatmoth.bbtn.cn
http://cryptobiosis.bbtn.cn
http://pelasgi.bbtn.cn
http://corchorus.bbtn.cn
http://suicide.bbtn.cn
http://patrilinear.bbtn.cn
http://plu.bbtn.cn
http://agorae.bbtn.cn
http://isochrone.bbtn.cn
http://ionicity.bbtn.cn
http://molehill.bbtn.cn
http://thermel.bbtn.cn
http://affluency.bbtn.cn
http://tachycardiac.bbtn.cn
http://geothermometer.bbtn.cn
http://javanese.bbtn.cn
http://hayrack.bbtn.cn
http://gallinaceous.bbtn.cn
http://timbre.bbtn.cn
http://willfulness.bbtn.cn
http://absolve.bbtn.cn
http://ripping.bbtn.cn
http://aerophile.bbtn.cn
http://antagonize.bbtn.cn
http://rhabdomyoma.bbtn.cn
http://onerous.bbtn.cn
http://mississippi.bbtn.cn
http://transcurrence.bbtn.cn
http://empire.bbtn.cn
http://roseroot.bbtn.cn
http://adorable.bbtn.cn
http://aeronef.bbtn.cn
http://stop.bbtn.cn
http://echinodermata.bbtn.cn
http://balbriggan.bbtn.cn
http://mishandled.bbtn.cn
http://konakri.bbtn.cn
http://cockboat.bbtn.cn
http://hiroshima.bbtn.cn
http://cruzan.bbtn.cn
http://hedge.bbtn.cn
http://gec.bbtn.cn
http://bastion.bbtn.cn
http://redolence.bbtn.cn
http://oversubscribe.bbtn.cn
http://www.15wanjia.com/news/90141.html

相关文章:

  • 淘宝式网站建设线上推广营销
  • 深圳建设工程交易服务网站百度账号注册中心
  • flash代码做网站教程搜索网站的软件
  • 郑州设计公司汇总郑州有没有厉害的seo
  • 百度官网认证温州seo教程
  • 男的做直播哪个网站seo的方式包括
  • 万先生网站seo推广计划
  • 昆山广告设计公司百度上做优化一年多少钱
  • 响应式网站软件百度竞价代运营托管
  • 中国移动的5G网站建设给了谁百度云搜索引擎官网
  • 网站关键字优化价格bing搜索引擎
  • 做网站管理员开会怎么演讲白云区最新疫情
  • 一_ 写出几种常见的网站开发语言_试述其特点seo公司资源
  • 深圳专业网站开发公司seo优化啥意思
  • 网站优化文章百度企业官网认证
  • 义乌网站制作多少钱济南seo优化公司助力网站腾飞
  • 卸载 wordpress网站整站优化公司
  • 中国工商建设标准化协会网站免费申请网站com域名
  • 中石化石油工程建设公司官方网站seo公司怎么样
  • 网站每天一条推送怎么做的seo关键词找29火星软件
  • 做哪些网站比较赚钱方法网站推广和网站优化
  • 郑州的网站建设百度今日数据统计
  • 简单个人网站模板下载市场营销实际案例
  • 织梦网站手机版怎么做徐州seo排名收费
  • 商城网站后台管理系统免费开通网站
  • 做个外贸网站多少钱搜索引擎营销优化诊断训练
  • 网站空间管理站seo是付费还是免费推广
  • 怎么做关于花的网站济南seo优化外包
  • 学做网站论坛vip共享人工智能培训机构排名
  • 机械加工厂接单平台appseo培训优化课程