I'm thinking of creating a web design summary site, and I'm looking for an easy way to create a web screen capture. For the time being, I implemented it with python and selenium.
pip install selenium
Download the file from here. Execute the following in the directory of the downloaded file.
$ mv chromedriver
import time
import re
from selenium import webdriver
#URL domain extraction pattern creation
pat = r"https?://(www.)?([\w-]+).[\w.]"
#Get URL from command line. ",Can be entered multiple times
inp = input("Enter the URL\n")
# 「,Divide into a list. Furthermore, the left and right blanks are erased
URLS = list(map(str.strip,(inp.split(","))))
#Browser launch (Chrome)
driver = webdriver.Chrome()
#Process URLs one by one from the list
for url in URLS :
#Set part of the domain as the file name
site_name = re.search(pat,url)
file_name = "{0}.png ".format(site_name.group(2))
#Open URL
driver.get(url)
#Set window size and zoom
driver.set_window_size(1250, 1036)
driver.execute_script("document.body.style.zoom='90%'")
#Read wait time
time.sleep(2)
#Save screenshot to images folder
driver.save_screenshot("./images/" + file_name)
#Close browser
driver.quit()
Recommended Posts