FunkyEssence
FunkyEssence

Reputation: 83

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.

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". I've tried a couple different ways with no luck. Does anyone have a solution to this?

Thanks!

Upvotes: 2

Views: 2290

Answers (3)

Aqeel Yasin
Aqeel Yasin

Reputation: 21

None of the above methods worked for me, I am using proxymesh proxies with selenium phantomJs python and following code snippet worked great.

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

driver = webdriver.PhantomJS(service_args=service_args) 

Upvotes: 0

FunkyEssence
FunkyEssence

Reputation: 83

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',
]

Upvotes: 1

gallen
gallen

Reputation: 1292

You could declare your service_args without the proxy variable, then append it afterward:

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.

Upvotes: 3

Related Questions