Loading

IP代理池之验证是否有效

IP代理池之验证是否有效

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

代码如下:

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!!!

posted @ 2019-05-23 21:06  YinJayChen  阅读(703)  评论(0编辑  收藏  举报