Downloading Pictures from Website

by Oct 31, 2016

There are awesome websites out there, and one is www.metabene.de (at least for German visitors)– on 33 pages, the artist presents his drawings for free download (private use).

In situations like this, PowerShell can help you automate the manual process of downloading pictures from a website. In PowerShell 3.0, a “PowerShell Browser” called Invoke-WebRequest was added that allows to automate most things a human would do with a real browser.

When you run this script, it visits all 33 pages, identifies all links to uploaded pictures, and saves them to your hard drive:

# open destination folder (and create it if needed)
$folder = 'c:\drawings'
$exists = Test-Path -Path $folder
if (!$exists) { $null = New-Item -Path $folder -ItemType Directory }
explorer $folder

# walk all 33 web pages that www.metabene.de offers
1..33 | ForEach-Object {
  $url = "http://www.metabene.de/galerie/page/$_"

  # navigate to website...
  $webpage = Invoke-WebRequest -Uri $url -UseBasicParsing
    
  # take sources of all images on this website...
  $webpage.Images.src |
  Where-Object {
    # take only images that were uploaded to this blog
    $_ -like '*/uploads/*'
  }
} |
ForEach-Object {
  # get filename of URL
  $filename = $_.Split('/')[-1]
  # create local file name
  $destination= Join-Path -Path $Folder -ChildPath $filename
  # download pictures
  Invoke-WebRequest -Uri $url -OutFile $destination
}

Twitter This Tip! ReTweet this Tip!