2013-05-14 151 views
-1

我有一些html代碼作爲字符串數據。現在,我需要以編程方式呈現代碼,以特定元素的大小截圖!如何在Python中截取html的截圖

從技術上講,我想在程序中執行一個網頁瀏覽器的東西..是否有可能?

我期待在Python環境

+2

也許[硒(https://pypi.python.org/pypi/selenium) – root 2013-05-14 06:29:41

+0

是硒的最佳解決方案,它有Python接口。 – specialscope 2013-05-14 06:49:11

+0

@specialscope您認爲硒可以在雲平臺上運行嗎?說,谷歌應用程序引擎,開放班等。(任何PaaS) – Iamcool 2013-05-14 10:42:54

回答

3

使用PySide或PyQt的,它的代碼相當的幾行:

UPDATE:固定碼:

from PySide.QtCore import QUrl, QTimer 
from PySide.QtGui import QApplication, QImage, QPainter 
from PySide.QtWebKit import QWebView 


class Browser(QWebView): 

    def __init__(self, app): 
     QWebView.__init__(self) 
     self.parent_app = app 
     self.loadFinished.connect(self._load_finished) 
     self.wait = 5 * 1000 # 5 secs 

    def _load_finished(self, ok): 
     if self.wait: 
      QTimer.singleShot(self.wait, lambda: self._load_finished(ok)) 
      self.wait = None 
      return 

     frame = self.page().mainFrame() 
     self.page().setViewportSize(frame.contentsSize()) 
     image = QImage(self.page().viewportSize(), QImage.Format_ARGB32) 
     painter = QPainter(image) 
     frame.render(painter) 
     painter.end() 
     image.save('test.png') 

     self.close() 
     self.parent_app.quit() 

    def open(self, url): 
     self.load(QUrl(url)) 


if __name__ == '__main__': 
    app = QApplication([]) 
    html = """ 
    <html> 
     <head> 
      <script type="text/javascript"> 
       setTimeout(function() { 
        var e = document.getElementById("later"); 
        e.innerHTML = "arrived"; 
       }, 2500); 
      </script> 
     </head> 
     <body> 
      <div id="later"></div> 
      <div style="margin: 0 auto; width: 500px;"> 
       <img src="http://www.caminodesantiago.me/wp-content/uploads/water-bottle.jpg" /> 
      </div> 
     </body> 
    </html> 
    """ 
    browser = Browser(app) 
    browser.setHtml(html) 
    app.exec_() 
+0

這似乎工作..但我有一個問題。在我的代碼中,JS定義了頁面的一些設計。並且它必須在腳本讀取圖像之前加載。 – Iamcool 2013-05-14 08:28:24

+0

加入'self.wait'不工作 – Iamcool 2013-05-14 09:03:01

+0

此外,這個腳本沒有終止 – Iamcool 2013-05-14 09:03:46

1

正如已經建議,硒的webdriver的Python綁定可能有所幫助。您的代碼可能是這樣的:

from selenium import webdriver 
from selenium.common.exceptions import WebDriverException 

browserHandler = webdriver.Firefox() 
browserHandler.get(yourUrl) 
try: 
    browserHandler.get_screenshot_as_file(yourPathToNewImage) 
except WebDriverException: 
    print("WebDriverException caught while trying to get a screenshot")