开源代码阅读——proxy_pool★

2022-04-04  本文已影响0人  我只要喝点果粒橙

项目链接: https://github.com/jhao104/proxy_pool

爬虫代理IP池项目,主要功能为定时采集网上发布的免费代理验证入库,定时验证入库的代理保证代理的可用性,提供API和CLI两种使用方式。同时你也可以扩展代理源以增加代理池IP的质量和数量。

模块组成:获取功能、存储功能、校验功能、接口管理

:star:程序主要是启动了startServer的API接口服务、startScheduler定时服务

  • startServer->runFlask:是向外提供了通过proxyHandler来获得Redis中的proxy数据
  • startScheduler->sche.add_task(__runProxyFetch)、sche.add_task(__runProxyCheck)
    • __runProxyFetch:proxy_fetcher.run()->proxy_queue->Checker("raw", proxy_queue)获得各个代理网站的代理信息后,进行校验,校验成功则入库
    • __runProxyCheck:proxy in proxy_handler.getAll()->proxy_queue->Checker("use", proxy_queue):通过proxy_handler拿到库里所有现存的数据后,进行有效性校验,无效的则删除,有效的则更新信息

作为存储功能的接口proxyHandler,也是两个API服务与定时服务的中介。程序也是通过存储功能,将核心的两个功能:<u>定时抓取的proxy数据</u>与<u>提供proxy数据给用户使用</u>成功联系在了一起

self.log.info("ProxyFetch : start")

# 从配置中拿执行函数
for fetch_source in self.conf.fetchers:
    # 判断ProxyFetcher中是否有定义、是否可调用
    fetcher = getattr(ProxyFetcher, fetch_source, None)
    if not fetcher:
        self.log.error("ProxyFetch - {func}: class method not exists!".format(func=fetch_source))
        continue
    if not callable(fetcher):
        self.log.error("ProxyFetch - {func}: must be class method".format(func=fetch_source))
        continue
    thread_list.append(_ThreadFetcher(fetch_source, proxy_dict))
    
for thread in thread_list:
    thread.setDaemon(True)
    thread.start()
for thread in thread_list:
    thread.join()
self.log.info("ProxyFetch - all complete!")
    

scheduler的逻辑

proxy_pool时序图.png

项目目录结构默写:

上一篇 下一篇

猜你喜欢

热点阅读