2017-06-21 98 views
2

我試圖驗證到使用PhantomJS與Python代理服務器。 這是我代理身份驗證與PhantomJS

service_args = [ 
     '--proxy=http://us-ny.proxymesh.com:31280', 
     '--proxy-type=http', 

] 

authentication_token = "Basic " + base64.b64encode(b'username:pass') 

capa = DesiredCapabilities.PHANTOMJS 
capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 

driver = webdriver.PhantomJS(desired_capabilities=capa, service_args=service_args) 

driver.get(request.url) 
body = driver.page_source 
print body 

這隻能打印出

<html><head></head><body></body></html>

只是爲了澄清,這作品時,添加我的IP到代理服務器 - 驗證的IP地址&主機名,但我需要它如果沒有

+1

兄我試圖用Python Selenium和PhantomJS配置代理,在Ubuntu和RedHat Linux上...花了3天,但代理沒有工作... – Umair

+1

哦,男孩,我真的很希望有一種方式 – morea030

回答

1

這裏是解決方案,我有工作。我最終需要通過在兩個service_args &作爲代理身份驗證標頭中的憑據。

service_args = [ 
    "--ignore-ssl-errors=true", 
    "--ssl-protocol=any", 
    "--proxy={}".format(proxy), 
    "--proxy-type=http", 
] 

caps = DesiredCapabilities.PHANTOMJS 

authentication_token = "Basic " + base64.b64encode(b'{}:{}'.format(username, password)) 

caps['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 

self.driver = webdriver.PhantomJS(
     service_args=service_args, 
     desired_capabilities=caps, 
     executable_path="./phantomjs-2.1.1-linux-x86_64/bin/phantomjs") 

,代理的結構被定義爲http://username:[email protected]:port

我不知道你爲什麼要重複的憑據。我猜測第一個auth參數不會作爲頭傳遞給代理,因此您需要手動執行這兩個操作。

0

你必須使用WebDriverWait等待驅動程序加載的網站,一旦完成打印的源代碼工作。

from selenium.webdriver.support.ui import WebDriverWait 
service_args = [ 
     '--proxy=http://us-ny.proxymesh.com:31280', 
     '--proxy-type=http', 

] 

authentication_token = "Basic " + base64.b64encode(b'username:pass') 

capa = DesiredCapabilities.PHANTOMJS 
capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 

driver = webdriver.PhantomJS(desired_capabilities=capa, service_args=service_args) 

driver.get(request.url) 
WebDriverWait(driver, *).until(lambda driver: driver.find_element_by_xpath(*) 
body = driver.page_source 
print body 

用你希望等待的元素的秒數xpath填充*。

+0

謝謝,但仍然沒有結果 – morea030