大数据 爬虫Python AI SqlPython小哥哥

Python最强的代理池,突破IP的封锁爬取海量数据!

2019-04-23  本文已影响1人  14e61d025165

一个强大到超乎你的想象的异步IP池项目——async-proxy-pool

随着大型网站反扒机制的增强,更改IP登陆已经成为一种最高效的方式,为此打造一款超强IP池项目,采用最新最快的Python技术——异步(Async )。编写了一个免费的异步爬虫代理池,以 Python asyncio 为基础,充分利用 Python 的异步性能,异步处理比同步处理能提升成百上千倍的效率,速度堪比GO语言。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239582" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

可以添加QQ群1004391443,有飞机大战、颜值打分器、打砖块小游戏、红包提醒神器、小姐姐表白神器等具体的实训项目,有清晰源码,有相应的文件

学习Python中的小伙伴,需要学习资料的话,可以前往我的微信公众号:速学Python,后台回复:简书,即可拿Python学习资料

这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。送给正在学习python的小伙伴!这里是python学习者聚集地,欢迎初学和进阶中的小伙伴!

项目介绍

本项目通过爬虫抓取互联网上免费代理网站的IP,并且进行异步检测是否可用,如果可用就放入数据库。定时对数据库中的代理进行维护,然后通过web api的形式供外部使用。

项目运行环境

项目使用了 sanic,一个异步网络框架。所以建议运行 Python 环境为 Python3.5+,并且 sanic 不支持 Windows 系统,Windows 用户可以考虑使用 Ubuntu on Windows。

总体架构

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239586" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

项目主要几大模块分别是爬取模块,存储模块,校验模块,调度模块,接口模块。

爬取模块crawler.py

负责爬取代理网站,并将所得到的代理存入到数据库,每个代理的初始化权值为 INIT_SCORE。

存储模块database.py

封装了 Redis 操作的一些接口,提供 Redis 连接池。

校验模块validator.py

验证代理 IP 是否可用,如果代理可用则权值 +1,最大值为 MAX_SCORE。不可用则权值 -1,直至权值为 0 时将代理从数据库中删除。

调度模块scheduler.py

负责调度爬取器和校验器的运行。

接口模块webapi.py

使用 sanic 提供 WEB API (服务器提供接口)。

如何使用

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239589" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

安装 Redis

项目数据库使用了 Redis,Redis 是一个开源(BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。所以请确保运行环境已经正确安装了 Redis。安装方法请参照官网指南。

项目源码

点击关注、转发、私信小编"史上最强代理池",就能免费获取强大的异步爬虫代理池项目源码。

安装依赖

$ pip install -r requirements.txt

使用API获取代理

运行代码后,访问http://127.0.0.1:5000/进入主页,如果显示'Welcome',证明成功启动。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239592" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

访问http://127.0.0.1:5000/get可以获取一个可用代理。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239595" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

也可以在程序代码中用相应的语言获取,例如:

import requests

from bs4 import BeautifulSoup

import lxml

def get_proxy():

r = requests.get('http://127.0.0.1:5000/get')

proxy = BeautifulSoup(r.text, "lxml").get_text()

return proxy

Sanic 性能测试

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556000239599" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

使用 wrk 进行服务器压力测试。基准测试 30 秒, 使用 12 个线程, 并发 400 个 http 连接。测试 http://127.0.0.1:3289/

$ wrk -t12 -c400 -d30s http://127.0.0.1:3289/

Running 30s test @ http://127.0.0.1:3289/

12 threads and 400 connections

Thread Stats Avg Stdev Max +/- Stdev

Latency 34.63ms 12.66ms 96.28ms 58.07%

Req/Sec 0.96k 137.29 2.21k 73.29%

342764 requests in 30.10s, 49.69MB read

Requests/sec: 11387.89

Transfer/sec: 1.65MB

:star:️ Requests/sec: 11387.89

我们看到了什么?平均 每秒钟11387.89个请求 ,就问你们怕不怕。机器性能更好一下,就是一波DOS攻击了。

实际代理性能测试

实测效果

https://taobao.com (测试淘宝)

测试代理: http://localhost:3289/get/20

测试网站: https://taobao.com/

测试次数: 1000

成功次数: 984

失败次数: 16

成功率: 0.984

https://baidu.com (测试百度)

测试代理: http://localhost:3289/get/20

测试网站: https://baidu.com

测试次数: 1000

成功次数: 975

失败次数: 25

成功率: 0.975

https://zhihu.com (测试知乎)

测试代理: http://localhost:3289/get/20

测试网站: https://zhihu.com

测试次数: 1000

成功次数: 1000

失败次数: 0

成功率: 1.0

可以看到其实性能是非常棒的,成功率极高。

写在最后

对于做爬虫的人来说,拥有一个免费的代理池是必须要。如果没有的话就需要去付费购买,有了这个项目源码之后,就相当于每个月省了至少是一百多块钱。同时Python的异步是未来的发展趋势,sanic异步框架天生就是处理 百万级高并发 的,如果你学会了这个项目出去找工作竞争是十分大的。

上一篇下一篇

猜你喜欢

热点阅读