如何在python中使用硒运行无头Chrome和代理? [英] How do you run headless chrome and a proxy using selenium in python?

查看:85
本文介绍了如何在python中使用硒运行无头Chrome和代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本具有无硒的chrome功能,但是如果可能的话,我又如何使用代理?如何将代理主机端口传递给我的无头Chrome浏览器?

My python script has the headless chrome in selenium up and functional but how, if possible, can I use a proxy as well? How can I pass the proxy host port to my headless chrome browser?

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

如何将代理主机端口与硒(主要是无头铬)一起使用?谢谢!

How do I use the proxy host port with selenium and mainly headless chrome? Thanks!

推荐答案

最简单的方法如下.将任何ip address放在proxy变量中以了解其工作方式. ipport:分隔.

The simplest way to do is something like below. Put any ip address in the proxy variable to find out how it works. ip and port are separated by :.

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

这篇关于如何在python中使用硒运行无头Chrome和代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆