Selenium代理设置

2024-09-24 06:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Python中通过代理使用Selenium。我的私有代理类型是:user:password:ip:port。下面的代码可以工作,但是没有userpassword选项。有人能更新这个代码吗?谢谢。你知道吗

from selenium import webdriver
import time


"Define Both ProxyHost and ProxyPort as String"
ProxyHost = "ip"
ProxyPort = "port"



def ChangeProxy(ProxyHost ,ProxyPort):
    "Define Firefox Profile with you ProxyHost and ProxyPort"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", ProxyHost )
    profile.set_preference("network.proxy.http_port", int(ProxyPort))
    profile.update_preferences()
    return webdriver.Firefox(firefox_profile=profile)


def FixProxy():
    "Reset Firefox Profile"
    profile = webdriver.FirefoxProfile()
    profile.set_preference("network.proxy.type", 0)
    return webdriver.Firefox(firefox_profile=profile)


driver = ChangeProxy(ProxyHost ,ProxyPort)
driver.get("http://whatismyipaddress.com")

time.sleep(5)

driver = FixProxy()
driver.get("http://whatismyipaddress.com")

Tags: http代理portdriverpasswordnetworkfirefoxprofile
1条回答
网友
1楼 · 发布于 2024-09-24 06:21:11

定义属性:

    username = "your_username_here" 
    password = "your_password_here"

然后将这些添加到您的首选项中:

    profile.set_preference("network.proxy.socks_username", username)
    profile.set_preference("network.proxy.socks_password", password)

另一种说法:

profile.set_preference('network.http.phishy-userpass-length', 255)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://username:password@somewebsite.com:port/")

相关问题 更多 >