如何安装PYQT,可以查看我的上一篇文章:http://www.cnblogs.com/liqiu/p/3361948.html
然后运行下面的带有JS程序的Python脚本即可:
#-*- coding:utf-8 -*- import sys import os.path import time from PyQt4 import QtGui,QtCore,QtWebKit class PageShotter(QtGui.QWidget): def __init__(self,url,parent=None): QtGui.QWidget.__init__(self,parent) self.url = url def shot(self): webView = QtWebKit.QWebView(self) self.webPage = webView.page() webView.load(QtCore.QUrl(self.url)) self.connect(webView,QtCore.SIGNAL("loadFinished(bool)"),self.savePage) def savePage(self,finished): #print finished if finished: print u"开始截图!" self.webPage.mainFrame().evaluateJavaScript("document.getElementById('kw').value='liqiu';"); size = self.webPage.mainFrame().contentsSize() print u"页面宽:%d,页面高:%d" % (size.width(),size.height()) self.resize(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= "shot.png"; 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.baidu.com/") shotter.shot() sys.exit(app.exec_())
这里面可能需要使用JS,修改页面的内容,上面红色的部分已经标出。
结果: