IP代理池之验证是否有效

IP代理池之验证是否有效

把proxy pool项目跑起来,但也不知道这些ip怎么用,爬虫的时候是否用代理去爬取,下面经过一个例子来看看。html

代码以下:python

import requests

PROXY_POOL_URL = 'http://127.0.0.1:5010/get/'

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


def get_html():
    try:
        r = requests.get('http://httpbin.org/get',proxies={'http':'http://%s' % get_proxy()})
        if r.status_code == 200:
            print(r.text)
    except:
        pass

for i in range(100):
    get_html()

先写个函数来请求IP代理池,获取到代理IP就赋予到proxies参数上,这时再写个for循环来证实是否是每次请求是不一样ip,http://httpbin.org/get 这个地址会返回请求信息 也就能看到是否用上代理 如今直接运行起来代码就ok了!函数

enter description here

上图说明已经成功用上代理IP!!!代理

相关文章
相关标签/搜索