Using and Randomizing Proxies

别说谁变了你拦得住时间么 提交于 2019-12-08 15:34:33

问题


I'm just wondering how you would go about setting a specific proxy for each request?!

The following block quote is the only thing the documentation says about this. Also, the documentation only provides an example for Java...

Firefox version 48 and newer - GeckoDriver
Firefox maintains its proxy configuration in a profile. You can preset the proxy in a profile and use that Firefox Profile or you can set it on profile that is created on the fly as is shown in the following example. With GeckoDriver the proxy has to be passed through the required capabilities.

Any advice would be appreciated!


回答1:


I've set proxies using PhantomJS before, but not using Firefox as the driver. Nonetheless, following this SO post's lead (repasting here for ease of use):

from selenium.webdriver.common.proxy import Proxy, ProxyType
myProxy = "xx.xx.xx.xx:xxxx"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'noProxy': '' # set this value as desired
    })
driver = webdriver.Firefox(proxy=proxy)
driver.get("http://www.google.com")

I would attempt to loop through a list of specified proxies and just modify (or re-create) the proxy variable on each request. If you want to randomize it, just call random.choice on the proxy list.




回答2:


I solved this by setting up proxies on the about:config page on Firefox. Here is the code that you need in order to do it:

devices = {
    "mobile" : "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0",
    "desktop" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"
}
scripts = 'var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); prefs.setIntPref("network.proxy.type", 1); prefs.setCharPref("network.proxy.socks", "' + proxy + '"); prefs.setIntPref("network.proxy.socks_port", port); prefs.setBoolPref("dom.webnotifications.enabled", false); prefs.setCharPref("general.useragent.override", "' + devices[device] + '");'

browser.execute_script(scripts)

If you dont want to override the UA then you dont need to use devices list and just remove the last js rule set in the script.



来源:https://stackoverflow.com/questions/51276893/using-and-randomizing-proxies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!