Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# this script takes multiple screenshots of a website
# including subsites and saves these screenshots into
# a folder starting with the name output_
# usage:
# python webscreenshot.py -u https://arminreiter.com
# python webscreenshot.py -u https://arminreiter.com -w 1920 -h 1080 -l 30
import argparse
import os
import urllib.request
from datetime import datetime
from time import sleep
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
def get_subsites(baseurl):
sites = ['/']
urls = urllib.request.urlopen('https://api.hackertarget.com/pagelinks/?q=' + baseurl).read()
for u in urls.decode('utf-8').splitlines():
rurl = u.replace(baseurl, '')
if u.startswith(baseurl) and not (rurl in sites):
sites.append(rurl)
return sites
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument("--url", "-u", help="url of the website, e.g. https://arminreiter.com", required=True)
parser.add_argument("--width", "-x", help="Screenshot resolution width, default: 2560", default=2560, type=int)
parser.add_argument("--height", "-y", help="Screenshot resolution height, default: 1440", default=1440, type=int)
parser.add_argument("--limit", "-l", help="Limits the number of urls that will be screenshoted (0 is no limit), default: 10", default=10, type=int)
args = parser.parse_args()
siteurl = args.url
screen_width = args.width
screen_height = args.height
output_directory = 'output_' + datetime.now().strftime('%Y%m%d_%H%M%S')
wait_time = 3 # time to wait until screenshot is taken
print("Start script for " + siteurl + ".")
print("Screenshot resolution: " + str(screen_width) + "x" + str(screen_height))
print("Save all files to: " + output_directory)
print("get subsites for " + siteurl)
sites = get_subsites(siteurl)
print("received " + str(len(sites)) + " sites")
print("start selenium webdriver")
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.set_window_size(screen_width, screen_height)
os.makedirs(output_directory , exist_ok=True)
total = len(sites)
if args.limit > 0:
total = args.limit
for i in range(total):
url = sites[i]
print(str(i+1).rjust(3,' ') + " of " + str(total) + ": get " + url + "...")
filename = url.replace('/','_') + ".png"
driver.get(siteurl + url)
sleep(wait_time)
outfile = os.path.join(output_directory, filename)
driver.get_screenshot_as_file(outfile)
driver.quit()
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment