简体   繁体   English

在 Python 中使用 PhantomJS 设置代理

[英]Set Proxy using PhantomJS in Python

So I have successfully set a proxy using the following code, and everything works.所以我使用下面的代码成功设置了一个代理,一切正常。 I would like to import a proxy automatically as a string and add the string to the service_args below, but I am unsure how to do this.我想将代理作为字符串自动导入并将该字符串添加到下面的 service_args 中,但我不确定如何执行此操作。

Current working code:当前工作代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

proxyIP = ('11.22.33.444')
proxyPort = ('5555')
proxy = ('{}:{}'.format(proxyIP, proxyPort))

service_args = [
    '--proxy=11.22.33.44:5555',
    '--proxy-type=http',
    '--ignore-ssl-errors=true',
]

browser = webdriver.PhantomJS(service_args=service_args)

Now, I want to be able to pass the "proxy" variable into where it says "--proxy=11.22.33.44:5555".现在,我希望能够将“代理”变量传递到它说“--proxy=11.22.33.44:5555”的地方。 I've tried a couple different ways with no luck.我尝试了几种不同的方法,但没有运气。 Does anyone have a solution to this?有没有人有解决方案?

Thanks!谢谢!

You could declare your service_args without the proxy variable, then append it afterward: 您可以声明不带proxy变量的service_args ,然后在其后追加:

service_args = [
    '--proxy-type=http',
    '--ignore-ssl-errors=true',
]
service_args.append(proxy)

Proxy would need to be a string as service_args is a list of strings. 代理服务器必须是字符串,因为service_args是字符串列表。

Thank you, your solution worked. 谢谢,您的解决方案成功了。 I did have a digit wrong in the proxy which is why I was having trouble. 我在代理中确实有一个数字错误,这就是我遇到麻烦的原因。 I ended up just doing: 我最终只是做:

service_args = [
'--proxy={}:{}'.format(proxyIP, proxyPort),
'--proxy-type=http',
'--ignore-ssl-errors=true',
]

None of the above methods worked for me, I am using proxymesh proxies with selenium phantomJs python and following code snippet worked great.以上方法都不适合我,我正在使用带有 selenium phantomJs python 的 proxymesh 代理,并且以下代码片段效果很好。

service_args=['--proxy=http://username:password@host:port',
              '--proxy-type=http',
              '--proxy-auth=username:password']

driver = webdriver.PhantomJS(service_args=service_args) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM