python网页截图

2020-01-06 11:22:51 浏览数 (1)

方法一、使用PyQt4的QtWebKit组件

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 来源 http://www.oschina.net/code/snippet_219811_14920
 
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("https://img.yuanmabao.com/zijie/pic/2020/01/06/r5xg1jqxaxo')
    shotter.shot()
    sys.exit(app.exec_())

方法二、使用selenium

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
 
import time
from selenium import webdriver
 
browser = webdriver.Firefox()
browser.set_window_size(1055, 800)
browser.get("https://img.yuanmabao.com/zijie/pic/2020/01/06/5xoeoperki5.png")
browser.quit()

0 人点赞