2013-08-01 16 views
12

Python'da otomatik tarayıcı simülasyonları ve web kazıma işlemleri için selenyum kullanıyorum ve benim için iyi çalıştı. Ama şimdi, onu bir proxy sunucusunun arkasına koymalıyım. Şimdi selenyum pencereyi açar, ancak açılmış tarayıcıda ayarlanmamış olan proxy ayarları nedeniyle istenen sayfayı açamadı. Şimdi de proxy sunucusu ile çalışmak için yukarıdaki kodu nasıl değiştirebilirimBir proxy sunucusunun arkasında çalışan selenyum

from selenium import webdriver 

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

: (örnek) şöyle Güncel kodudur?

+0

: aşağıdakileri yapmanız gerekir (sizin örnek kod tercih edilen tarayıcı olduğunu) Firefox için //stackoverflow.com/a/38168865/5409601 –

cevap

21

Böyle istenilen yetenekleri veya tarayıcı profili ayarlamak gerekir:

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) 

Ayrıca bakınız ilgili konuları:

+0

Önerdiğinizi denedim ama hala proxy sunucusunu geçemedim – Aryabhatt

+0

Tercih güncellemesini yaptıktan sonra selenium tarafından açılan tarayıcı ayarlarını kontrol ettim. Aslında sorun şu ki, http_port'u doğru bir şekilde ayarlamıyor (ve onu 0 olarak bırakıyor), çünkü bağlanmadı. Liman ortamında herhangi bir sorun var mı? – Aryabhatt

+0

Hm, bir sayı (dize değil) olarak ayarlamayı deneyebilir misiniz? – alecxe

8

Resmi Selenium belgeleri (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy) bir proxy kullanma konusunda açık ve yardımcı yönergeler sağlar.

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

Bu işi yapacak: Eğer bu http deneyebilirsiniz

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

Kullanıcı eklemek için: pass – Umair

+1

Dikkat edilmesi gereken önemli noktalar: Eğer proxyHost bir hostname ise _ "http: //" _ prefix içermemelidir – 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)