Get web screen capture with python

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.

Premise

Preparation

Install selenium

pip install selenium

Install Chrome Webdriver

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

Get web screen capture with python
Get date with python
Get country code with python
Web scraping with python + JupyterLab
Get Twitter timeline with python
Camera capture with Python + OpenCV
Web API with Python + Falcon
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Web application with Python + Flask ② ③
Get stock price with Python
Web scraping beginner with python
Get home directory with python
Get keyboard events with python
Streamline web search with python
Get Alembic information with Python
Web application with Python + Flask ④
Python beginners get stuck with their first web scraping
Link to get started with python
Web scraping with Python ① (Scraping prior knowledge)
Getting Started with Python Web Applications
Monitor Python web apps with Prometheus
Get the weather with Python requests
Get the weather with Python requests 2
[Python] Get economic data with DataReader
How to get started with Python
[Small story] Get timestamp with Python
Get Qiita trends with Python scraping
Get started with Python in Blender
Get weather information with Python & scraping
Get US stock price from Python with Web API with Raspberry Pi
Operate Firefox with Selenium from python and save the screen capture
Get additional data in LDAP with python
Get property information by scraping with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
WEB scraping with Python (for personal notes)
[Development environment] Python with Xcode [With screen transition]
Getting Started with Python Web Scraping Practice
[Python] Get the variable name with str
Shared screen screenshot exe app with python
[Python] [Windows] Take a screen capture in Python
Daemonize a Python web app with Supervisor
[Personal note] Web page scraping with python3
Web scraping with Python ② (Actually scraping stock sites)
Download files on the web with Python
Horse Racing Site Web Scraping with Python
[Python] A quick web application with Bottle!
Getting Started with Python Web Scraping Practice
Easy web app with Python + Flask + Heroku
Get Started with TopCoder in Python (2020 Edition)
Run a Python web application with Docker
Let's make a web framework with Python! (1)
[Python] Get Python package information with PyPI API
Practice web scraping with Python and Selenium
Easy web scraping with Python and Ruby
How Python beginners get started with Python with Progete
Let's get along with Python # 0 (Environment construction)
Let's make a web framework with Python! (2)
[For beginners] Try web scraping with Python
[Blender x Python] Let's get started with Blender Python !!