python 实现网页长截屏

比较早的Chrome浏览器可以支持ctrl+M 保存整个网页为图片,个人很喜欢这个功能,遇到漂亮的图片可以保存起来慢慢欣赏,但是后来就用不了。

最近在看python爬虫,发现了一种实现方法,在这里介绍一下:

主要原理是Phantomjs浏览器的功能,需要安装selenium工具。

selenium可以用pip直接安装

pip install selenium

phantomjs需要到官网或者镜像网站下载,然后吧.exe文件拷贝到python路径中的scripts文件夹。

具体的代码如下所示:

from selenium import webdriver
from datetime import datetime

driver = webdriver.PhantomJS()
begintime=datetime.now()
print("visiting  "+'guancha.cn')
driver.get("http://www.guancha.cn")
title = driver.title
print("title is "+title)
print('saving screen ing')
driver.save_screenshot('guancha.png')
endtime=datetime.now()
stime=endtime-begintime
print('saved! total time(s):'+str(stime.seconds))

猜你喜欢

转载自blog.csdn.net/u013894427/article/details/78422214