使用chromedriver實現豆瓣網頁的全網頁截圖


    最近由於工作需要,需要對部分網站進行全文截屏。在網上搜了很久沒有搜出好的方法,而且在新版的selenium中已經不在支持PhantomJS了,所以全文截取變得有點棘手。

    此處給出一個簡單的方法,以20行代碼實現selenium+chromedriver+python實現豆瓣的全文截取。

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--dns-prefetch-disable')
options.add_argument('--no-referrers')
options.add_argument('--disable-gpu')
options.add_argument('--disable-audio')
options.add_argument('--no-sandbox')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-insecure-localhost')

driver = webdriver.Chrome(options=options)
driver.get('http://www.douban.com')
width = driver.execute_script(
        "return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
height = driver.execute_script(
        "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
driver.set_window_size(width + 100, height + 100)
driver.save_screenshot('douban.png')
driver.close()

以下為截屏效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM