2016-04-20 21 views
5

Voglio catturare il traffico verso i siti che sto cercando di usare Selenium con python e dato che il traffico sarà https usando un proxy non mi farò allontanare.Cattura della rete con Selenium/PhantomJS

La mia idea era di eseguire phantomJS con selenio e utilizzare phantomJS per eseguire uno script (non sulla pagina utilizzando webdriver.execute_script(), ma su phantomJS stesso). Stavo pensando allo script netlog.js (da qui https://github.com/ariya/phantomjs/blob/master/examples/netlog.js).

Dal momento che funziona in questo modo nella riga di comando

phantomjs --cookies-file=/tmp/foo netlog.js https://google.com 

ci deve essere un modo simile a che fare questo con il selenio?

Grazie in anticipo

Aggiornamento:

risolto con browsermob-proxy.

pip3 install browsermob-proxy 

codice python3

from selenium import webdriver 
from browsermobproxy import Server 

server = Server(<path to browsermob-proxy>) 
server.start() 
proxy = server.create_proxy({'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True}) 

service_args = ["--proxy=%s" % proxy.proxy, '--ignore-ssl-errors=yes'] 
driver = webdriver.PhantomJS(service_args=service_args) 

proxy.new_har() 
driver.get('https://google.com') 
print(proxy.har) # this is the archive 
# for example: 
all_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']] 
+1

Inoltre per installare la libreria python con 'pip', è necessario anche scaricare l'ultima versione di bmp da' https: // github.com/lightbody/browsermob-proxy/releases' e installare l'ambiente di runtime java 'apt-get install default- jre'. 'viene quindi impostato sul percorso di bmp scaricato. – isedwards

risposta

5

Sto usando un proxy per questo

from selenium import webdriver 
from browsermobproxy import Server 

server = Server(environment.b_mob_proxy_path) 
server.start() 
proxy = server.create_proxy() 
service_args = ["--proxy-server=%s" % proxy.proxy] 
driver = webdriver.PhantomJS(service_args=service_args) 

proxy.new_har() 
driver.get('url_to_open') 
print proxy.har # this is the archive 
# for example: 
all_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']] 

il (formato http archivio) 'har' ha un sacco di altre informazioni circa le richieste e le risposte , mi è molto utile

installazione su Linux:

pip install browsermob-proxy 
+0

Grazie, lo ha fatto anche così. Sebbene per Python3 sia necessario modificare un codice e un parametro phantomJS. Aggiornato nel mio post. – Bart

+0

ha usato 'driver = webdriver.Chrome (service_args = service_args)' invece ha funzionato come un incantesimo – Indra

1

Per questo utilizzo una soluzione senza un server proxy. Ho modificato il codice sorgente del selenio in base al collegamento qui sotto per aggiungere la funzione executePhantomJS.

https://github.com/SeleniumHQ/selenium/pull/2331/files

Poi ho eseguire il seguente script dopo aver ottenuto il driver phantomJS:

from selenium.webdriver import PhantomJS 

driver = PhantomJS() 

script = """ 
    var page = this; 
    page.onResourceRequested = function (req) { 
     console.log('requested: ' + JSON.stringify(req, undefined, 4)); 
    }; 
    page.onResourceReceived = function (res) { 
     console.log('received: ' + JSON.stringify(res, undefined, 4)); 
    }; 
""" 

driver.execute_phantomjs(script) 
driver.get("http://ariya.github.com/js/random/") 
driver.quit() 

Poi tutte le richieste vengono registrate nella console (in genere il file ghostdriver.log)