PhantomJS 滚动截取整个网页图片(防止页面懒加载)
2021-05-12 本文已影响0人
隐墨留白
下载PhantomJS
Download PhantomJSphantomjs.org
安装PhantomJS,下载包解压后,把文件夹bin中的phantomjs.exe移到python文件夹中的Scripts中
代码实现:
from selenium import webdriver
import time
driver = webdriver.PhantomJS()
driver.get("http://mp.weixin.qq.com/xxxxxxxxxxxxxx")
time.sleep(2)
# 获取网页高度值
height = driver.execute_script("return document.documentElement.scrollHeight")
print(height)
# 滚动幅度,如果图片比较窄,数值可以减少
skip = 500
# 滚动条滚动
for i in range(0, height + skip, skip):
js = 'document.body.scrollTop={}'.format(i)
driver.execute_script(js)
# 等待图片加载时间,看网速
time.sleep(1)
driver.save_screenshot('test.png')
driver.quit()