RegalCoding.com

A Programming Blog

Hello mobile user!

Batch Screen Capture of Websites

Today I found myself needing to create screenshots of a rather large list of websites, nearly 200 of them in total. Doing this manually would take far too long, so some type of automation was required.

In the past I've performed this task using WebShot and although it used to work very well, it hasn't been updated in many years and appears to have problems with some newer websites. After looking around a bit I couldn't find anything else that seemed to be working adequately, however I did learn that Google's Chrome browser can make screencaps when called from the command line; it was a simple matter to write a batch file and generate those screencaps automatically.

Here's how I did it:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --enable-logging --window-size=1024,768 --hide-scrollbars --screenshot="path\to\image.png" http://example.com


That one line could be typed into the command line, but since I had a list of urls to capture I just added them all, one per line, to a text file and saved it with a .bat extension. In Windows, the .bat extension turns the file into what is known as a batch file. Running that file causes the commands within to execute.

Although this worked well, I did find that some websites still required manual intervention; a number of sites in my list had modal coupons and so forth which I wanted to dismiss before capturing, and a few of them had redirects which caused Chrome, for some reason, to capture at the landing url rather than the destination url. This was not a consistent problem, some worked and some didn't. Regardless, even with having to redo about 10% of the list by hand, this saved me a ton of time.

There were a couple of minor issues; this method does not resize the captured images, and it can only save them as .png files, so I had to use other programs to take care of those minor tasks. This website BulkResizePhotos.com helped me out quite a bit with that task.

Category: General Coding