python高质量免费IP代理池,一键部署小白也会(附源码)

一:前言

        详细大家在写爬虫时或多或少都遇到过ip封锁的问题,网上的免费ip代理网站不少但质量比较底下,很多延迟高,或者已经失效了。今天给大家分享的是我再GitHub上看到的一个比较不错的代理池,完全免费,可用性高。

二:IP池介绍

     该IP代理池具有以下几个特点:

  • 定时抓取免费代理网站,简易可扩展。
  • 使用 Redis 对代理进行存储并对代理可用性进行排序。
  • 定时测试和筛选,剔除不可用代理,留下可用代理。
  • 提供代理 API,随机取用测试通过的可用代理。

三:使用方法

        ProxyPool可以通过两种方式来运行代理池,一种方式是使用 Docker(推荐),另一种方式是常规方式运行,要求如下:

Docker(建议在Linux服务器部署)

如果使用 Docker,则需要安装如下环境:

  • Docker
  • Docker-Compose

安装方法自行搜索即可。

官方 Docker Hub 镜像:germey/proxypool

常规方式

常规方式要求有 Python 环境、Redis 环境,具体要求如下:

  • Python>=3.6
  • Redis

如果安装好了 Docker 和 Docker-Compose,只需要一条命令即可运行。

docker-compose up

运行结果类似如下:

redis        | 1:M 19 Feb 2020 17:09:43.940 * DB loaded from disk: 0.000 seconds
redis        | 1:M 19 Feb 2020 17:09:43.940 * Ready to accept connections
proxypool    | 2020-02-19 17:09:44,200 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
proxypool    | 2020-02-19 17:09:44,203 INFO supervisord started with pid 1
proxypool    | 2020-02-19 17:09:45,209 INFO spawned: 'getter' with pid 10
proxypool    | 2020-02-19 17:09:45,212 INFO spawned: 'server' with pid 11
proxypool    | 2020-02-19 17:09:45,216 INFO spawned: 'tester' with pid 12
proxypool    | 2020-02-19 17:09:46,596 INFO success: getter entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
proxypool    | 2020-02-19 17:09:46,596 INFO success: server entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
proxypool    | 2020-02-19 17:09:46,596 INFO success: tester entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

可以看到 Redis、Getter、Server、Tester 都已经启动成功。

这时候访问 http://localhost:5555/random 即可获取一个随机可用代理。

当然你也可以选择自己 Build,直接运行如下命令即可:

docker-compose -f build.yaml up

如果下载速度特别慢,可以自行修改 Dockerfile,修改:

- RUN pip install -r requirements.txt
+ RUN pip install -r requirements.txt -i https://pypi.douban.com/simple

由于篇幅原因,另一种运行方式再次不多赘述。想了解更多的可以去 GitHub - Python3WebSpider/ProxyPool: An Efficient ProxyPool with Getter, Tester and Server

下载链接:

不想去GitHub下载项目的可以通过这个链接下载:ProxyPool

四:运行效果

部署成功即可请求相应的网址获取一个免费的IP,博主亲测可用率在95%以上,每次刷新都会得到一个新的IP。

五:python实战效果

源码:

# coding:utf-8
import requests

PROXY_POOL_URL = 'http://你的ip:5555/random'

def get_proxy():
    try:
        response = requests.get(PROXY_POOL_URL)
        if response.status_code == 200:
            return response.text
    except ConnectionError:
        return None

while True:
    proxy = get_proxy()
    proxies = {
        'http': 'http://' + proxy,
        'https': 'http://' + proxy,
    }
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0'}
    try:
        response = requests.get('http://httpbin.org/get', proxies=proxies, timeout=10)
        print(response.text)
        res= requests.get('https://www.baidu.com', proxies=proxies, timeout=5000, headers=headers)
        res.encoding=res.apparent_encoding
        print(res.text)
        break
    except Exception as e:
        print(e)

猜你喜欢

转载自blog.csdn.net/weixin_61736939/article/details/130876550