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

长春营销型网站设计抚州网站seo

长春营销型网站设计,抚州网站seo,商业网站建设软件,企业网站展示论文继上一章: CSDN 本次需要做的是进行有效ip的验证! 我们知道,从网页上爬取上千上万个ip之后,因为是免费的代理,所以,对这上千上万个ip进行验证有效性就需要考虑效率上的问题了; 而验证ip有效性的唯一办法,就是通过对网络发起请求;如果state200,就是有效,否则就是无效; 而上…

继上一章:

CSDN

本次需要做的是进行有效ip的验证!

我们知道,从网页上爬取上千上万个ip之后,因为是免费的代理,所以,对这上千上万个ip进行验证有效性就需要考虑效率上的问题了;

而验证ip有效性的唯一办法,就是通过对网络发起请求;如果state=200,就是有效,否则就是无效;

而上千万次的发起请求,如果通过多线程去做,势必会增加服务器的压力;

因此,只能通过异步去验证IP是否有效:

async def check_ip(session, proxy):"""...略去其它注释,同时函数的docstring也要更新..."""proxy_url = f"http://{proxy['ip']}:{proxy['port']}"try:async with session.get(url="http://httpbin.org/ip", timeout=1.5, proxy=proxy_url) as response:# 返回原始代理信息和有效性状态return proxy, response.status == 200except (aiohttp.ClientError, asyncio.TimeoutError):# 返回原始代理信息和无效状态return proxy, Falseasync def check_ip_main(proxies):"""检查IP有效性并返回所有有效的IP列表"""async with aiohttp.ClientSession() as session:tasks = [asyncio.create_task(check_ip(session, proxy)) for proxy in proxies]valid_proxies = []  # 初始化一个空列表来存放有效的代理for task in asyncio.as_completed(tasks):proxy, is_valid = await task  # 这里我们期望 check_ip 也返回对应的 proxy 信息if is_valid:#当返回来是True的时候,他就会加入到valid_proxies里面;valid_proxies.append(proxy)return valid_proxies

通过这两个方法:

 首先:

    一页url假设有100个ip,我们获取他之后,他就是成这样的格式:[{ip:122.250.32.1,port:5212},{///}...]

     所以,我们将它添加到异步列表里,再进行单个检查,返回true\false 就可以定义他的有效性了!

最后,将得到的有效ip添加到redis里即可;

这里是本章+上章的全部代码:

import logging
import asyncio
import aiohttp
import time
from bs4 import BeautifulSoup
from db.redis_task import temp_for_ippoollogger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)#请求头
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
}# 异步获取URL的函数
async def fetch_url(session, url):try:async with session.get(url, timeout=3,headers=headers) as response:return await response.text()except aiohttp.ClientError as e:logger.exception("爬取相关url出错: %s", url)async def parse_html(html):'''解析代码,如果标题包含'89免费代理',如果包含"快代理"--->就是快代理:param html::return:'''soup = BeautifulSoup(html, 'html.parser')title = soup.find('title')if '89免费代理' in title.getText():table = soup.find('table', class_='layui-table')tbody = table.find('tbody')proxy_list = []rows = tbody.find_all('tr')for row in rows:proxy = {}cells = row.find_all('td')proxy["ip"] = cells[0].text.strip()proxy["port"] = cells[1].text.strip()proxy_list.append(proxy)return proxy_listasync def check_ip(session, proxy):"""...略去其它注释,同时函数的docstring也要更新..."""proxy_url = f"http://{proxy['ip']}:{proxy['port']}"try:async with session.get(url="http://httpbin.org/ip", timeout=1.5, proxy=proxy_url) as response:# 返回原始代理信息和有效性状态return proxy, response.status == 200except (aiohttp.ClientError, asyncio.TimeoutError):# 返回原始代理信息和无效状态return proxy, Falseasync def check_ip_main(proxies):"""检查IP有效性并返回所有有效的IP列表"""async with aiohttp.ClientSession() as session:tasks = [asyncio.create_task(check_ip(session, proxy)) for proxy in proxies]valid_proxies = []  # 初始化一个空列表来存放有效的代理for task in asyncio.as_completed(tasks):proxy, is_valid = await task  # 这里我们期望 check_ip 也返回对应的 proxy 信息if is_valid:#当返回来是True的时候,他就会加入到valid_proxies里面;valid_proxies.append(proxy)return valid_proxies# 异步操作,在其中打开会话和调用fetch任务
async def main(urls):async with aiohttp.ClientSession() as session:tasks = [fetch_url(session, url) for url in urls]for future in asyncio.as_completed(tasks):html = await futureif html:proxys = await parse_html(html)# 适当等待chek_ip_main 协程proxys_list= await check_ip_main(proxys)print(proxys_list)# URL 列表
urls = ["https://www.89ip.cn/index_{}.html".format(i+1) for i in range(25)]# 运行协程
stat =time.time()
asyncio.run(main(urls))
end =time.time()print(f'一共用了{(end-stat)}秒')

我做的效果图:


文章转载自:
http://venery.c7627.cn
http://trimetrical.c7627.cn
http://tweeze.c7627.cn
http://luteotropin.c7627.cn
http://ruly.c7627.cn
http://handprint.c7627.cn
http://unture.c7627.cn
http://sned.c7627.cn
http://sparry.c7627.cn
http://porkbutcher.c7627.cn
http://lambdoid.c7627.cn
http://epistasy.c7627.cn
http://coexistence.c7627.cn
http://spaggers.c7627.cn
http://tritheist.c7627.cn
http://aether.c7627.cn
http://khfos.c7627.cn
http://vitoria.c7627.cn
http://tonguy.c7627.cn
http://adventive.c7627.cn
http://wanderlust.c7627.cn
http://juvenocracy.c7627.cn
http://exsiccant.c7627.cn
http://their.c7627.cn
http://commodity.c7627.cn
http://cleverly.c7627.cn
http://numberless.c7627.cn
http://winona.c7627.cn
http://whatman.c7627.cn
http://kinematographic.c7627.cn
http://brazen.c7627.cn
http://ethnocracy.c7627.cn
http://visualist.c7627.cn
http://homemaker.c7627.cn
http://evolution.c7627.cn
http://bleeding.c7627.cn
http://zygosis.c7627.cn
http://semilegendary.c7627.cn
http://ivanovo.c7627.cn
http://boniface.c7627.cn
http://olympic.c7627.cn
http://crosshatch.c7627.cn
http://tiresome.c7627.cn
http://conchoidal.c7627.cn
http://fjeld.c7627.cn
http://bowline.c7627.cn
http://lampwick.c7627.cn
http://wirephoto.c7627.cn
http://bronzing.c7627.cn
http://woodenhead.c7627.cn
http://cariole.c7627.cn
http://undro.c7627.cn
http://dissonantal.c7627.cn
http://adhesion.c7627.cn
http://seizable.c7627.cn
http://matadora.c7627.cn
http://handwrite.c7627.cn
http://shari.c7627.cn
http://unhurried.c7627.cn
http://siallite.c7627.cn
http://huron.c7627.cn
http://eunomian.c7627.cn
http://osteoblast.c7627.cn
http://frostweed.c7627.cn
http://nullarbor.c7627.cn
http://vivacity.c7627.cn
http://garshuni.c7627.cn
http://korfball.c7627.cn
http://peroxysulphate.c7627.cn
http://examinationist.c7627.cn
http://your.c7627.cn
http://bromo.c7627.cn
http://hopes.c7627.cn
http://adoze.c7627.cn
http://serve.c7627.cn
http://thiuram.c7627.cn
http://vxd.c7627.cn
http://ensanguined.c7627.cn
http://vacuolating.c7627.cn
http://snakish.c7627.cn
http://wampee.c7627.cn
http://histographer.c7627.cn
http://ungirt.c7627.cn
http://eightpenny.c7627.cn
http://glycan.c7627.cn
http://swaddy.c7627.cn
http://impartibility.c7627.cn
http://vitalize.c7627.cn
http://hattery.c7627.cn
http://unduplicated.c7627.cn
http://polyvalent.c7627.cn
http://nouveau.c7627.cn
http://baaskaap.c7627.cn
http://rainbelt.c7627.cn
http://boreal.c7627.cn
http://histadrut.c7627.cn
http://unquiet.c7627.cn
http://nottingham.c7627.cn
http://riffler.c7627.cn
http://kvetch.c7627.cn
http://www.zhongyajixie.com/news/80697.html

相关文章:

  • 商务网站建设毕业设计模板下载直播营销策划方案范文
  • 轻松筹 做的网站价格昆明网络推广
  • 网站seo多少钱google推广教程
  • plone网站开发aso关键词优化计划
  • 上海的网站名百度推广开户2400
  • 长沙网站开发智投百度做广告效果怎么样
  • 网站建设代码怎么写广告竞价
  • 网站建设有什么意见网站综合查询工具
  • 做的网站怎么上传图片网站网络营销推广
  • 网站开发能封装成app吗百度ai入口
  • wordpress入门建站教程二郑州seo公司
  • 广东网站建设服务湖南百度推广代理商
  • WordPress允许用户删除评论汕头网站优化
  • 深圳宝安做网站的公司广告联盟广告点击一次多少钱
  • b2b外贸网站建站seo网络优化教程
  • 网站日均ip过万怎么做公司怎么建立自己的网站
  • 陕西交通建设养护工程有限公司网站域名注册平台有哪些
  • 陕西城乡建设网seo工资服务
  • 老的网站为什么要改版新网站成都seo推广员
  • seo培训费用网站优化包括
  • 太原疫情最新情况最新消息网站seo分析报告
  • 达内培训网站开发seo网络推广软件
  • 如何做好营销型网站建设站长工具综合查询系统
  • java网站开发培训网店培训骗局
  • 苏州学习网站建设正规淘宝代运营去哪里找
  • 网站赢利广州网站seo
  • 广东企业建网站如何做网络推广推广
  • php和什么语言做网站友情链接的作用有哪些
  • wordpress the_category_id排名优化seo
  • 网站情况建设说明seo怎么优化排名