簡體   English   中英

使用python,selenium和phantomJS設置代理

[英]Setting a proxy using python, selenium, and phantomJS

我已經嘗試過由Alex在下一頁上發布的解決方案,但我一直收到此錯誤。 如何在python webdriver中為phantomjs / ghostdriver設置代理?

我的PATH中有phantomJS。

File "C:\Users\sri19\Desktop\Gui OSRS\testphantomproxy.py", line 21, in <module>
  driver = webdriver.PhantomJS(service_args=phan_args, desired_capabilities=dcap)
File "C:\Python27\Lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 52, in __init__
  self.service.start()
File "C:\Python27\Lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
  self.assert_process_still_running()
File "C:\Python27\Lib\site-packages\selenium\webdriver\common\service.py", line 99, in assert_process_still_running
  % (self.path, return_code)

selenium.common.exceptions.WebDriverException: Message: Service phantomjs unexpectedly exited. Status code was: -1

這是我當前的測試腳本

from selenium import webdriver
from selenium.webdriver.common.proxy import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
import sys

singleproxy = "88.157.149.250:8080"
proxytype = "http"

user_agent = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36")

phan_args = ['--proxy=88.157.149.250:8080', 'proxy-type=http']
print "step 1"
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent
print "step 2"
driver = webdriver.PhantomJS(service_args=phan_args, desired_capabilities=dcap)
driver.get("https://www.whatismyip.com/")
print "step 3"
print driver.current_url

htmlpage = driver.page_source
print htmlpage.encode(sys.stdout.encoding, errors='replace')

誰能幫助我了解為什么我收到此錯誤? 當我將此代理應用於chromedriver(通過另一種方法)時,它起作用了。 因此它不能是代理。

小子...犯了一個小錯誤。

以下行phan_args = ['--proxy = 88.157.149.250:8080','proxy-type = http']應該是phan_args = ['--proxy = 88.157.149.250:8080','--proxy-type = http']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM