Самая быстрая итерация прокси в Python - PullRequest
0 голосов
/ 03 апреля 2020

Допустим, у меня есть список, который содержит 10 000+ прокси

proxy_list = ['ip:port','ip:port',.....10,000+ items]

Как мне выполнить итерацию, чтобы получить прокси, которые работают для моего p c? Используя следующий код, его можно найти, но для его завершения требуется 5 * 10000 секунд. Как бы я перебрал список быстрее?

import requests
result=[]
for I in proxy_list:
    try:
        requests.get('http:\\www.httpbin.org\ip',proxies = {'https' : I, 'http' : I } ,timeout = 5)
        result.append(I)
    except:
        pass

1 Ответ

1 голос
/ 03 апреля 2020

Вы можете использовать многопоточность, это позволит программе проверять несколько прокси одновременно.

import requests
import threading
import concurrent.futures

appendLock = threading.Lock() """This is to keep multiple threads from appending 
to the list at the same time"""

workers = 10 """This is the number of threads that will iterate through your proxy list.
In my experience, increasing this number higher than 30 causes problems."""

proxy_list = ['ip:port','ip:port',.....10,000+ items]

result = []

def proxyCheck(proxy):
    try:
        requests.get('http://www.httpbin.org/ip',proxies = {'https' : I, 'http' : I } ,timeout = 5)
        with appendLock:
            result.append(I)
    except:
        pass

with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
    for proxy in proxy_list:
        executor.submit(proxyCheck(proxy))
...