2016-05-30 75 views
2

我想用Python中的Selenium使用PhantomJS(在Windows中)做一些網頁抓取。
我已經從網站上下載了最新的PhantomJS版本,然後解壓。從那以後,我已經試過來自硒的PhantomJS - Python

from selenium import webdriver 
browser = webdriver.PhantomJS() 

的反應是

WebDriverException: 'phantomjs' executable needs to be in PATH. 

然後我試圖添加路徑,像

browser = webdriver.PhantomJS('path_to/phantomjs.exe') 

我也試圖把一個' r'之前的路徑。的反應是在HTML的exxeption:

WebDriverException: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Notification: Gateway Timeout</title> 

<style type="text/css"> 
body { 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 14px; 
    color:#333333; 
    background-color: #ffffff; 
} 
h1 { 
    font-size: 18px; 
    font-weight: bold; 
    text-decoration: none; 
    padding-top: 0px; 
    color: #2970A6; 
} 
a:link { 
    color: #2970A6; 
    text-decoration: none; 
} 
a:hover { 
    color: #2970A6; 
    text-decoration: underline; 
} 
p.buttonlink { 
    margin-bottom: 24px; 
} 
.copyright { 
    font-size: 12px; 
    color: #666666; 
    margin: 5px 5px 0px 30px; 

} 
.details { 
    font-size: 14px; 
    color: #969696; 
    border: none; 
    padding: 20px 20px 20px 20px; 
    margin: 0px 10px 10px 35px; 
} 

.shadow { 
    border: 3px solid #9f9f9f; 
    padding: 10px 25px 10px 25px; 
    margin: 10px 35px 0px 30px; 
    background-color: #ffffff; 
    width: 600px; 

    -moz-box-shadow: 3px 3px 3px #cccccc; 
    -webkit-box-shadow: 3px 3px 3px #cccccc; 
    box-shadow: 3px 3px 3px #cccccc; 
    /* For IE 8 */ 
    -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=135, Color='cccccc')"; 
    /* For IE 5.5 - 7 */ 
    filter: progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=135, Color='cccccc'); 
} 
.logo { 
    border: none; 
    margin: 5px 5px 0px 30px; 
} 
</style> 

</head> 

<body> 
<div class="logo"></div><p>&nbsp;</p> 
<div class="shadow"> 
<h1>This Page Cannot Be Displayed</h1> 


<p> 
The system cannot communicate with the external server (&nbsp;127.0.0.1&nbsp;). 
The Internet server may be busy, may be permanently down, or may be 
unreachable because of network problems. 
</p> 

<p> 
Please check the spelling of the Internet address entered. 
If it is correct, try this request later. 
</p> 



<p> 
If you have questions, please contact 
your corporate network administrator 
and provide the codes shown below. 
</p> 

</div> 

<div class="details"><p> 
Date: Mon, 30 May 2016 12:30:14 CEST<br /> 
Username: <br /> 
Source IP: 10.202.210.98<br /> 
URL: POST http://127.0.0.1/wd/hub/session<br /> 
Category: Uncategorized URLs<br /> 
Reason: UNKNOWN<br /> 
Notification: GATEWAY_TIMEOUT 
</p></div> 
</body> 
</html> 

我已經開這個代碼到Chrome會話,它打開我公司的防火牆頁。消息是「系統無法與外部服務器通信(127.0.0.1)」。我可以使用Chrome或Firefox驅動程序進行網頁瀏覽,但PhantomJS存在此問題。
你能幫我嗎?

+0

我也發現使用的URL是POST http://127.0.0.1/wd/hub/session。有沒有辦法改變這個?這顯然是企業防火牆的一個問題。 –

回答

2

嘗試如下您已經安裝在何處使用絕對路徑,也可以設置「NO_PROXY」環境「127.0.0.1」

os.environ['NO_PROXY'] = '127.0.0.1' 
driver = webdriver.PhantomJS(
executable_path=r'C:\Python\Python35-32\Lib\site-packages\phantomjs-2.1.1-windows\bin\phantomjs') 
+0

不,同樣的問題。 –

0

我試圖讓這個在Windows下工作,也...

我超類WebDriver,並且當初始化超類時,我將可執行文件所在的ABSOLUTE路徑傳遞給webdriver的__init__方法。

這讓我進一步的 - 現在我看到

Exception WebDriverException: Message: 'phantomjs' executable may have wrong permissions. 

這給我的印象,我在正確的軌道上......這意味着你可以只通過該路徑的可執行文件位於成webdriver對象的構造函數(作爲一個簡單的字符串)。