2016-09-04 23 views
6

私はセレニウム+ファントムを使用してWebスクレイピングのプロキシを設定しようとしています。私はPythonを使用しています。python proxy-authのphantomjs + seleniumが動作しません

私は、phantomjsにバグがあり、proxy-authが動作しないことが多くの場所で分かっています。

page.customHeaders = { 'プロキシ許可': '基本 '+ btoa(' USERNAME:PASSWORD')}

from selenium.webdriver.common.proxy import * 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
service_args = [ 
'--proxy=http://fr.proxymesh.com:31280', 
'--proxy-auth=USER:PWD', 
'--proxy-type=http', 
] 

driver = webdriver.PhantomJS(service_args=service_args) 
driver.get("https://www.google.com") 
print driver.page_source 

プロキシメッシュは、代わりに以下を使用することを提案します。

しかし、私はそれをどのようにPythonに変換するのか分かりません。

これは私が現在持っているものです。

from selenium import webdriver 
import base64 
from selenium.webdriver.common.proxy import * 
from selenium import webdriver 
from selenium.webdriver.common.by import By 

service_args = [ 
'--proxy=http://fr.proxymesh.com:31280', 
'--proxy-type=http', 
] 

headers = { 'Proxy-Authorization': 'Basic ' + base64.b64encode('USERNAME:PASSWORD')} 

for key, value in enumerate(headers): 
    webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.{}'.format(key)] = value 

driver = webdriver.PhantomJS(service_args=service_args) 
driver.get("https://www.google.com") 
print driver.page_source 

をしかし、それは動作しません。

どのように私はこれを動作させるための任意の提案?

+0

あなたはセレン及びPhantomJSを使用する必要がありますか? Webスクレイピングの場合、より柔軟なオプションが必要です。 –

+0

私はjavscriptウェブサイトを擦る必要があります。他に何が使えるのかの提案はありますか? – chris

+0

この場合、より良い提案はありません。 –

答えて

4

私は答えからコンパイルしています: How to correctly pass basic auth (every click) using Selenium and phantomjs webdriver を同様に: base64.b64encode error

from selenium import webdriver 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
import base64 

service_args = [ 
    '--proxy=http://fr.proxymesh.com:31280', 
    '--proxy-type=http', 
] 

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

capa = DesiredCapabilities.PHANTOMJS 
capa['phantomjs.page.customHeaders.Proxy-Authorization'] = authentication_token 
driver = webdriver.PhantomJS(desired_capabilities=capa, service_args=service_args) 

driver.get("http://...") 
+0

信じられない。これに何週間も執着していた。ありがとう! – chris

+0

大歓迎です! – jinksPadlock

+0

賞金を受け取ったのですか?私は前にそれを使用したことがないので、私はあなたに恩恵を与えるために他の何かをしなければならないかどうかわかりません。 – chris

3

DesiredCapabilitiesと解決策が私のために動作しませんでした。 私は、次の解決策を終了している:

from selenium import webdriver 

driver = webdriver.PhantomJS(executable_path=config.PHANTOMJS_PATH, 
service_args=['--ignore-ssl-errors=true', 
    '--ssl-protocol=any', 
    '--proxy={}'.format(self.proxy), 
    '--proxy-type=http', 
    '--proxy-auth={}:{}'.format(self.proxy_username, self.proxy_password)]) 
関連する問題