先在首页上执行一段 JavaScript 脚本,将页面的滚动条拖到最下方,然后再拖回顶部,最后才截图。这样可以解决那种按需加载图片的情况
以下代码为转载别处博客改造后的,有chrome和ff两种浏览器。
由于webdriver.chrom只能截取当前页面图片,不能截取整个网页图片,因此额外使用webdriver.PhantomJS,需要下载PhantomJS,将PhantomJS.exe放到python的script目录下
from selenium import webdriver import time def capture(browser_name,url,save_fn): #save_fn = save_fn + ".png" save_fn = url.split("://")[1].replace('/',' ') + ".png" #print save_fn if browser_name == 'firefox' or browser_name == 'Firefox' or browser_name == 'ff': driver = webdriver.Firefox() driver.maximize_window() elif browser_name == 'chrome' or browser_name == 'Chrome': #options = webdriver.chrome.options.Options() #options.add_argument("--start-maximized") #driver = webdriver.Chrome(chrome_options=options) driver = webdriver.PhantomJS() driver.maximize_window() else: raise AssertionError("browser name is error,firefox/Firefox/ff/chrome/Chrome") driver.get(url) # Load page driver.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 50); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1000); })(); """) for i in xrange(30): if "scroll-done" in driver.title: break time.sleep(1) driver.save_screenshot(save_fn) driver.close() if __name__ == "__main__": capture("ff","https://www.cnblogs.com/yes123/p/5279843.html")
每天努力一点,每天学习一点。
Keep Moving...