2012-09-30 34 views
3

我要帶許多網頁的截圖,我寫了這個:Python Splinter(SeleniumHQ)如何截取很多網頁的截圖? [連接被拒絕]

from splinter.browser import Browser 
import urllib2 
from urllib2 import URLError 

urls = ['http://ubuntu.com/', 'http://xubuntu.org/'] 


try : 
    browser = Browser('firefox') 
    for i in range(0, len(urls)) : 
     browser.visit(urls[i]) 
     if browser.status_code.is_success() : 
      browser.driver.save_screenshot('your_screenshot' + str(i) + '.png') 
     browser.quit() 
except SystemError : 
    print('install firefox!') 
except urllib2.URLError, e: 
    print(e) 
    print('theres no such website') 
except Exception, e : 
    print(e) 
    browser.quit() 

,我得到這個錯誤:

<urlopen error [Errno 111] Connection refused> 

如何解決它:)

編輯

當我在txt文件中有鏈接時,下面的代碼不起作用:

from splinter import Browser 
import socket 

urls = [] 
numbers = [] 

with open("urls.txt", 'r') as filename : 
    for line in filename : 
     line = line.strip() 
     words = line.split("\t") 
     numbers.append(str(words[0])) 
     urls.append(str(words[1].rstrip())) 

print(urls) 

browser = None  
try: 
    browser = Browser('firefox') 
    for i, url in enumerate(urls, start=1): 
     try: 
      browser.visit(url) 
      if browser.status_code.is_success(): 
       browser.driver.save_screenshot('your_screenshot_%03d.png' % i) 
     except socket.gaierror, e: 
      print "URL not found: %s" % url 
finally: 
    if browser is not None: 
     browser.quit() 

我的txt文件看起來是這樣的:

1 http//ubuntu.com/ 
2 http//xubuntu.org/ 
3 http//kubuntu.org/ 

,當我跑了,我得到了錯誤:

$ python test.py 
['http//ubuntu.com/', 'http//xubuntu.org/', 'http//kubuntu.org/'] 
Traceback (most recent call last): 
    File "test.py", line 21, in <module> 
    browser.visit(url) 
    File "/usr/local/lib/python2.7/dist-packages/splinter/driver/webdriver/__init__.py", line 79, in visit 
    self.driver.get(url) 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 168, in get 
    self.execute(Command.GET, {'url': url}) 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: u'Component returned failure code: 0x804b000a (NS_ERROR_MALFORMED_URI) [nsIIOService.newURI]' 

什麼錯了?

+0

第一步 - 只是嘗試使用瀏覽器「正常」打開頁面,看看是否有效... –

+0

@JonClements:它工作時,我只有一個鏈接,但當我有更多,我得到這個錯誤: ( – Katie

+0

是的,嘗試打開'http // ubuntu.com /'並祈禱。'NS_ERROR_MALFORMED_URI'明確表示URL不正確。 – erm3nda

回答

6

你的問題是你在你的循環中通過URL做browser.quit(),所以它不再打開第二個URL。

這裏是你的代碼的更新版本:

from splinter import Browser 
import socket 

urls = ['http://ubuntu.com/', 'http://xubuntu.org/'] 

browser = None  
try: 
    browser = Browser('firefox') 
    for i, url in enumerate(urls, start=1): 
     try: 
      browser.visit(url) 
      if browser.status_code.is_success(): 
       browser.driver.save_screenshot('your_screenshot_%03d.png' % i) 
     except socket.gaierror, e: 
      print "URL not found: %s" % url 
finally: 
    if browser is not None: 
     browser.quit() 

主要的變化是移動browser.quit()代碼到你的主要異常處理程序的finally,所以它會不管發生什麼不順心。還請注意使用enumerate來提供迭代器值及其索引;這是Python在維護自己的索引指針方面的推薦方法。

我不確定它是否與您的代碼相關,但我發現splinterurllib2.URLError之間產生了socket.gaierror例外,所以我展示瞭如何將它們陷入困境。我在循環中移動了這個異常處理程序;即使其中一個或多個URL不存在,這也將繼續獲取剩餘的屏幕截圖。

+0

非常感謝您(我必須先測試它),但我認爲這正是我需要的!Thaaaanks:* – Katie

+0

我有這個代碼的一些問題:(你能這麼友好,看看我的編輯?我粘貼了一個新的版本,我有* .txt文件中的鏈接,但它不想工作:/ – Katie

+0

一般而言,建議您開始一個新問題以關注新問題。 –