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

相關文章
相關標籤/搜索