简体   繁体   English

Python Selenium Phantomjs,在运行时设置代理

[英]Python Selenium Phantomjs, set proxy at runtime

I am aware that I can set proxy settings for phantomjs on initialization using service_args but restarting phantomjs every time just to change proxy setting seems wasteful. 我知道我可以使用service_args在初始化时为phantomjs设置代理设置,但每次只是为了更改代理设置而重新启动phantomjs似乎很浪费。 In javascript changing proxy at runtime would be done with setProxy function. 在javascript中,运行时更改代理将使用setProxy函数完成。 How can I make this work in Python using selenium? 如何使用selenium在Python中完成这项工作?

Trying various options and reading a bit of code, I realized that it is possible to dynamically change proxies in python + selenium + phantomjs. 尝试各种选项并阅读一些代码,我意识到可以在python + selenium + phantomjs中动态更改代理。 For posterity here is a sample code: 对于后代,这里有一个示例代码:

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
driver.execute('executePhantomScript', {'script': '''phantom.setProxy("10.0.0.1", 80);''', 'args' : [] })

Happy ghosting ;) 快乐重影;)

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

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