2013-08-01 29 views
12

Ho utilizzato il selenio per le simulazioni automatiche del browser e il web scraping in python e ha funzionato bene per me. Ma ora, devo eseguirlo dietro un server proxy. Quindi ora il selenio apre la finestra ma non è possibile aprire la pagina richiesta a causa delle impostazioni del proxy non impostate nel browser aperto. codice attuale è la seguente (esempio):Selenium in esecuzione dietro un server proxy

from selenium import webdriver 

sel = webdriver.Firefox() 
sel.get('http://www.google.com') 
sel.title 
sel.quit() 

Come faccio a cambiare il codice di cui sopra per lavorare con server proxy ora pure?

+0

si può provare questo http://stackoverflow.com/a/38168865/5409601 –

risposta

21

è necessario impostare le funzionalità desiderata o profilo del browser, in questo modo:

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1) 
profile.set_preference("network.proxy.http", "proxy.server.address") 
profile.set_preference("network.proxy.http_port", "port_number") 
profile.update_preferences() 
driver = webdriver.Firefox(firefox_profile=profile) 

anche vedere le discussioni correlati:

+0

Ho provato quello che hai suggerito ma non riesco ancora a superare il server proxy – Aryabhatt

+0

Ho controllato le impostazioni del browser aperte dal selenio dopo aver effettuato l'aggiornamento delle preferenze. In realtà il problema è che, non sta impostando http_port correttamente (e lasciandolo a 0), a causa del quale non si sta connettendo. Ci sono problemi nell'impostazione della porta? – Aryabhatt

+0

Hm, puoi provare a impostarlo come numero (non stringa)? – alecxe

8

La documentazione ufficiale sul selenio (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy) fornisce linee guida chiare e utili sull'utilizzo di un proxy. Per Firefox (che è il browser di scelta nel codice di esempio) si dovrebbe effettuare le seguenti operazioni:

from selenium import webdriver 
from selenium.webdriver.common.proxy import * 

myProxy = "host:8080" 

proxy = Proxy({ 
    'proxyType': ProxyType.MANUAL, 
    'httpProxy': myProxy, 
    'ftpProxy': myProxy, 
    'sslProxy': myProxy, 
    'noProxy': '' # set this value as desired 
    }) 

driver = webdriver.Firefox(proxy=proxy) 
3

Questo farà il lavoro:

import selenium 
from selenium.webdriver.common.proxy import * 

proxyHost = "my.proxy.host or IP" 
proxyPort = "55555" 

fp = webdriver.FirefoxProfile() 
fp.set_preference("network.proxy.type", 1) 
#fp.set_preference("network.proxy.http", proxyHost) #HTTP PROXY 
#fp.set_preference("network.proxy.http_port", int(proxyPort)) 
#fp.set_preference("network.proxy.ssl", proxyHost) #SSL PROXY 
#fp.set_preference("network.proxy.ssl_port", int(proxyPort)) 
fp.set_preference('network.proxy.socks', proxyHost) #SOCKS PROXY 
fp.set_preference('network.proxy.socks_port', int(proxyPort)) 
fp.update_preferences() 

driver = webdriver.Firefox(firefox_profile=fp) 

driver.get("http://www.whatismyip.com/") 
+1

come aggiungere utente: passare – Umair

+1

Importante da notare: se 'proxyHost' è un nome host non deve contenere il prefisso _" http: // "_ – salvob

0
def install_proxy(PROXY_HOST,PROXY_PORT): 
    fp = webdriver.FirefoxProfile() 
    print PROXY_PORT 
    print PROXY_HOST 
    fp.set_preference("network.proxy.type", 1) 
    fp.set_preference("network.proxy.http",PROXY_HOST) 
    fp.set_preference("network.proxy.http_port",int(PROXY_PORT)) 
    fp.set_preference("network.proxy.https",PROXY_HOST) 
    fp.set_preference("network.proxy.https_port",int(PROXY_PORT)) 
    fp.set_preference("network.proxy.ssl",PROXY_HOST) 
    fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT)) 
    fp.set_preference("network.proxy.ftp",PROXY_HOST) 
    fp.set_preference("network.proxy.ftp_port",int(PROXY_PORT)) 
    fp.set_preference("network.proxy.socks",PROXY_HOST) 
    fp.set_preference("network.proxy.socks_port",int(PROXY_PORT)) 
    fp.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A") 
    fp.update_preferences() 
    return webdriver.Firefox(firefox_profile=fp) 
Problemi correlati