Selenium Threads:如何使用代理运行多线程浏览器(python)

万通

我正在编写脚本以使用具有多个线程的代理访问网站,但是现在我陷入了多个线程中,当我在下面运行脚本时,它会打开5个浏览器,但全部5个都使用1个代理,我希望5个浏览器使用不同的代理代理,有人可以帮我完成吗?谢谢

这是我的脚本:

from selenium import webdriver
from selenium import webdriver
import time , random
import threading


def e():

    a = open("sock2.txt", "r")
    for line in a.readlines():

        b = line
        prox = b.split(":")
        IP = prox[0]
        PORT = int(prox[1].strip("\n"))
        print(IP)
        print(PORT)


        profile = webdriver.FirefoxProfile()
        profile.set_preference("network.proxy.type", 1)
        profile.set_preference("network.proxy.socks", IP)
        profile.set_preference("network.proxy.socks_port", PORT)
        try:

            driver = webdriver.Firefox(firefox_profile=profile)
            driver.get("http://www.whatsmyip.org/")
        except:
            print("Proxy Connection Error")
            driver.quit()
        else:
            time.sleep(random.randint(40, 70))
            driver.quit()
for i in range(5):
    t = threading.Thread(target=e)
    t.start()

(祝大家新年快乐,万事如意)

沙拉德

DominikLašo正确地捕获了它-每个线程从头开始处理文件。大概是这样的:

from selenium import webdriver
from selenium import webdriver
import time , random
import threading


def e(ip, port):
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.socks", IP)
    profile.set_preference("network.proxy.socks_port", PORT)
    try:
        driver = webdriver.Firefox(firefox_profile=profile)
        driver.get("http://www.whatsmyip.org/")
    except:
        print("Proxy Connection Error")
        driver.quit()
    else:
        time.sleep(random.randint(40, 70))
        driver.quit()

my_threads = []
with open("sock2.txt", "r") as fd:
    for line in fd.readlines():
        line = line.strip()
        if not line:
           continue
        prox = line.split(":")
        ip = prox[0]
        port = int(prox[1])
        print('-> {}:{}'.format(ip, port))
        t = threading.Thread(target=e, args=(ip, port,))
        t.start()
        my_threads.append(t)

for t in my_threads:
    t.join()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用无头浏览器运行Selenium Python

使用 Python 的 Selenium 多线程

如何使用Selenium Webdriver和浏览器HtmlunitDriver for Java设置代理?

使用Selenium WebDriver运行TOR浏览器

Selenium运行错误的浏览器(默认)

在Python中,如何通过保存的浏览器会话使Selenium正常运行?

我如何在谷歌浏览器中运行这个 python+ selenium webdriver 代码?

Python、selenium - 如何在不显示浏览器窗口的情况下运行脚本

如何以多线程方式在Python中使用Selenium

Python Selenium:如何使浏览器窗口不打开

Python Selenium如何加载浏览器的数据(cookie或书签)

Python selenium 如何保持浏览器打开?

如何通过Selenium在Chrome浏览器中使用Tor

如何使用 Selenium 更改浏览器(Chrome)的语言?

Testcontainers Selenium 如何更新浏览器?

Python Selenium保持浏览器打开

PHP Selenium Firefox 浏览器代理 socks5

python selenium中的多线程

Python Selenium多线程问题

如何在python,selenium和chromedriver中使用Brave Web浏览器?

如何让 chromedriver 记住我使用 python selenium 通过浏览器启动应用程序的设置?

如何使用 selenium WebDriver 和 python 打开功能齐全的 Chrome 浏览器?

如何使用Selenium和python在Tor浏览器中单击按钮

如何使用Selenium和Python启动基于Chromium的Vivaldi浏览器会话

如何使用 python 和 Selenium 在浏览器中保存 cookie

在Linux上使用无头Chrome浏览器访问“拒绝”页面,而带头Chrome浏览器在Windows上使用Selenium通过Python在Windows上运行

在docker容器中运行selenium chrome浏览器

通过 TestNG 在不同浏览器上运行 selenium 测试

在 MacOs (Catalina) 中在 Selenium 上运行 Edge 浏览器