我想使用多线程解决方案执行以下代码
有人可以建议如何改善解决方案吗?
from selenium import webdriver
with open('proxy.txt', 'r') as f:
for line in f:
print ("Connected with IP: {}".format(line))
PROXY = line # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://whatismyipaddress.com")
driver.quit()
答案 0 :(得分:1)
import os
from multiprocessing import Pool
from selenium import webdriver
def check_ip(proxy):
print ("Connected with IP: {}".format(proxy))
options = webdriver.chrome.options.Options()
options.add_argument('--proxy-server=http://{}'.format(proxy))
driver = webdriver.Chrome(options=options)
driver.get("http://whatismyipaddress.com")
driver.quit()
if __name__ == '__main__':
with open('./proxy.txt') as f:
proxies = f.read().splitlines()
with Pool() as pool:
pool.map(check_ip, proxies)
解决方案基于this question
中的答案