你如何在python中使用selenium运行无头chrome和代理?

问题描述 投票:1回答:2

我的python脚本在selenium中具有无头镀铬功能但是如果可能的话,我也可以使用代理?如何将代理主机端口传递给无头镀铬浏览器?

options = webdriver.ChromeOptions()  
options.add_argument('headless')  
browser = webdriver.Chrome(chrome_options=options)

如何使用代理主机端口与selenium和主要无头铬?谢谢!

python selenium web-scraping proxy headless
2个回答
0
投票

最简单的方法是如下所示。将任何ip address放在proxy变量中,以了解它是如何工作的。 ipport:分开。

from selenium import webdriver

proxy = "94.122.251.105:3128" #test with any ip address which supports `http` as well because the link within the script are of `http`

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--proxy-server={}'.format(proxy))
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get('http://www.lagado.com/proxy-test')
items = driver.find_element_by_css_selector(".main-panel p:nth-of-type(2)").text
print(items) #it should print out the ip address you are using in the `proxy` variable above
driver.quit()

1
投票

像这样的东西:

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType


options = webdriver.ChromeOptions()  
options.add_argument('headless')
desired_caps = options.to_capabilities()

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"
prox.add_to_capabilities(desired_caps)


browser = webdriver.Chrome(desired_capabilities=desired_caps)
© www.soinside.com 2019 - 2024. All rights reserved.