从ip代理池ProxyPool中获取一个可以ip

代理池存储在Redis数据库中,以Web API形式随机获取一个可用代理

import requests

PROXY_POOL_URL = 'http://localhost:5555/random'  # Web形式


def get_proxy():
    # 获取代理函数
    try:
        response = requests.get(PROXY_POOL_URL)
        if response.status_code == 200:
            return response.text
    except ConnectionError:
        return None

proxy = get_proxy()

print(proxy)
proxies = {
    'http': 'http://' + proxy,
    'https': 'https://' + proxy,
}

try:
    response = requests.get('http://httpbin.org/get', proxies=proxies)
    print(response.text)
except requests.exceptions.ConnectionError as e:
    print('Error', e.args)

猜你喜欢

转载自blog.csdn.net/qq_17249717/article/details/81774655
今日推荐