使用python对整个网页进行截图

Posted hello2020888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python对整个网页进行截图相关的知识,希望对你有一定的参考价值。

方法一、使用PyQt4的QtWebKit组件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
 
import sys
import os.path
from PyQt4 import QtGui,QtCore,QtWebKit
 
class PageShotter(QtGui.QWidget):
    def __init__(self,url,filename,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.url = url
        self.filename = filename
        self.webpage = None
 
    def shot(self):
        webview = QtWebKit.QWebView(self)
        webview.load(QtCore.QUrl(self.url))
        self.webpage = webview.page()
        self.connect(webview,QtCore.SIGNAL("loadFinished(bool)"),self.save_page)
 
    def save_page(self,finished):
        #print finished
        if finished:
            print u"开始截图!"
            size = self.webpage.mainFrame().contentsSize()
            print u"页面宽:%d,页面高:%d" % (size.width(),size.height())
            self.webpage.setViewportSize(QtCore.QSize(size.width()+16,size.height()))
            img = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
            painter = QtGui.QPainter(img)
            self.webpage.mainFrame().render(painter)
            painter.end()
            filename= self.filename;
            if img.save(filename):
                filepath = os.path.join(os.path.dirname(__file__), filename)
                print u"截图完毕:%s" % filepath
            else:
                print u"截图失败";
        else:
            print u"网页加载失败!"
        self.close()
 
if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)
    #shotter = PageShotter("http://www.adssfwewfdsfdsf.com")
    shotter = PageShotter("http://www.youku.com/", ‘shot.png‘)
    shotter.shot()
    sys.exit(app.exec_())

 

 

方法二、使用selenium

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
 
import time
from selenium import webdriver
 
browser = webdriver.Firefox()
browser.set_window_size(1055, 800)
browser.get("http://www.yooli.com/")
browser.find_element_by_id("idClose").click()
time.sleep(5)
 
browser.save_screenshot("shot.png")
browser.quit()

以上是关于使用python对整个网页进行截图的主要内容,如果未能解决你的问题,请参考以下文章

python使用上下文对代码片段进行计时,非装饰器

Mac的正确打开方式

Python简单实现阴阳师挂机脚本

python怎么抓取网页中DIV的文字

python实现网页/HTML截图并转PDF

python实现网页/HTML截图并转PDF