如何在python中执行多线程硒程序?

时间:2019-01-17 22:43:59

标签: python multithreading selenium multiprocessing python-multithreading

我想使用多线程解决方案执行以下代码

有人可以建议如何改善解决方案吗?

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()

1 个答案:

答案 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

中的答案