IP代理池之验证是否有效

发布时间:2019-06-07 20:35:07编辑:auto阅读(1813)

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

关键字

上一篇: day 44 前端HTML

下一篇: 社区发现