Python 3 Selenium Web Scraping Script to Take Screenshot of Website URL and Download it as PNG Image File Full Project For Beginners

Python 3 Selenium Web Scraping Script to Take Screenshot of Website URL and Download it as PNG Image File Full Project For Beginners

 

Welcome folks today in this post we will be fetching screenshot of website url and download it as png image file using selenium library in python .All the full source code of application is shown below.

 

 

 

Get Started

 

 

In order to get started we need to install the following library using the pip command as shown below

 

pip install selenium

 

pip install chromedriver_binary==XX.X.XXXX.XX.X

 

Where you will put your chrome version number inside xx characters

 

After this you need to make an app.py file and copy paste the following code

 

app.py

 

 

# -*- cofing: utf-8 -*-
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import chromedriver_binary


script_name = sys.argv[0]

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

try:
    url = sys.argv[1]

    driver.get(url)
    page_width = driver.execute_script('return document.body.scrollWidth')
    page_height = driver.execute_script('return document.body.scrollHeight')
    driver.set_window_size(page_width, page_height)
    driver.save_screenshot('screenshot.png')
    driver.quit()
    print("SUCCESS")

except IndexError:
    print('Usage: %s URL' % script_name)

 

 

Now to run this python script using the command line you will pass an additional command line argument which will be the url of the website to download the screenshot of the image as png file

See also  Python 3 Kivy Sudoku Puzzle Solver Script GUI Desktop App Game Using Numpy Library Full Project For Beginners

 

python app.py https://www.google.com

 

Now it will download the google.com website screenshot as shown below in the same directory

 

 

 

 

 

Leave a Reply