Python Script : Take screenshot
Python Code to Take Screenshot of WebPage
Python Code to Take Screenshot of WebPage
n today's digital era, automation plays a crucial role in simplifying tasks and improving efficiency. One such task is capturing screenshots of web pages . In this blog post, we will explore how to achieve this using Python.
Requirements
To follow along with the code examples, ensure that you have the following installed:
Python (version 3 or above)
Selenium library
BeautifulSoup library
Chrome WebDriver
Setting Up the Environment
Before we dive into the code, we need to set up our Python environment. First, let's install the required libraries by running the following commands:
Copy code
pip install selenium beautifulsoup4
Next, download the Chrome WebDriver from the official website and place it in a directory accessible by your Python script.
Taking Screenshots of URLs
To capture screenshots of multiple URLs, we will utilize the powerful selenium library. This library allows us to automate web browsing and interact with web pages programmatically. Let's take a look at the code:
python
Copy code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
# List of URLs to capture screenshots
urls = [
"https://www.example1.com",
"https://www.example2.com",
"https://www.example3.com"
]
# Set up the Chrome driver with headless option
chrome_options = Options()
chrome_options.add_argument("--headless") # Run Chrome in headless mode
driver = webdriver.Chrome(options=chrome_options)
# Visit each URL, take a screenshot
for url in urls:
driver.get(url)
time.sleep(2) # Wait for the page to load (you can adjust the time if needed)
# Capture the screenshot
screenshot_path = f"screenshot_{time.time()}.png"
driver.save_screenshot(screenshot_path)
# Close the driver
driver.quit()
In the code snippet above, we start by importing the necessary modules. We define a list of URLs for which we want to capture screenshots. Then, we set up the Chrome driver in headless mode, which allows us to run Chrome without a visible browser window. We iterate through the URLs, visit each page using driver.get(url), and wait for the page to load using time.sleep(2). You can adjust the waiting time according to the page loading speed. Finally, we capture a screenshot using driver.save_screenshot(screenshot_path) and save it with a unique filename.
def take_screenshot(url, output_file):
browser = webdriver.Chrome(options=chrome_options,service=ChromeService(ChromeDriverManager().install()))
browser.implicitly_wait(500)
browser.get(url)
print(url)
time.sleep(20) # Wait for the page to load completely
browser.save_screenshot(output_file)
browser.quit()
# List of web UIs to capture
web_pages = [
{
'url': 'https://vocal.media/01',
'filename': 'vocal.png'
},
{
'url': 'https://www.google.com',
'filename': 'google.png'
}
]
# Email details
sender_email = '[email protected]'
sender_password = '<password>'
receiver_email = '[email protected]'
subject = 'Web UI Screenshots'
body = 'Please find the screenshots of the web UIs attached.'
# Capture screenshots and send emails
for page in web_pages:
url = page['url']
filename = page['filename']
take_screenshot(url, filename)
#send_email(sender_email, sender_password, receiver_email, subject, body, filename)
Extracting Content from Web Pages
Taking screenshots is useful, but to generate meaningful blog content, we also need to extract information from the web pages. For this purpose, we'll utilize the BeautifulSoup library, a powerful tool for parsing HTML and XML documents. Let's modify our code to extract content:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import time
# List of URLs to capture screenshots and extract content
urls = [
"https://www.example1.com",
"https://www.example2.com",
"https://www.example3.com"
]
# Set up the Chrome driver with headless option
chrome_options = Options()
chrome_options.add_argument("--headless") # Run Chrome in headless mode
driver = webdriver.Chrome(options=chrome_options)
# Visit each URL, take a screenshot, and extract content
for url in urls:
driver.get(url)
time.sleep(2) # Wait for the page to load (you can adjust the time
About the Creator
Arun Ramasamy
Nature Lover, Just go with the flow, techno freek.
Do what you can.. don't when you cannot.
Comments
There are no comments for this story
Be the first to respond and start the conversation.