利用selenium时,chrome和firfox使用代理ip

    xiaoxiao2021-04-12  39

    ip是为“182.90.80.137:8123”相同格式的字符串

    火狐浏览器:

    ip_ip = ip.split(":")[0] ip_port = int(ip.split(":")[1]) print(ip_ip) print(ip_port) random_header = random.choice(HEADERS) webdriver.DesiredCapabilities.FIREFOX['firefox.page.settings.userAgent'] = random_header profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 1) # 默认值0,就是直接连接;1就是手工配置代理。 profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) profile.set_preference('network.proxy.ssl', ip_ip) profile.set_preference('network.proxy.ssl_port', ip_port) profile.update_preferences() driver = webdriver.Firefox(profile)

    谷歌浏览器

    chromedriver = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe' chome_options = webdriver.ChromeOptions() chome_options.add_argument(('--proxy-server=http://' + ip)) os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver, chrome_options=chome_options)

    转载请注明原文地址: https://ju.6miu.com/read-667230.html

    最新回复(0)