Download photos from Rightmove using PowerShell

Whether you are interested in buying, or selling, property via Rightmove, you might want to download the images. Now sure, you can do this one-by-one, by right clicking on each photo (when viewed full screen) and choose to copy/save the photos, but that’s a very manual task, which will take you time.

This type of manual work is fine as a one off, but to do it regulary is very time consuming and boring.

The good news is, I’ve made it easy for you by writing the below PowerShell script. I’ll talk you through what it does, but if you just want the end result, skip to the end of this article.

PowerShell ask for a URL

So the script needs to know what the URL to the rightmove page is, so to do this, we just ask for user input using the Read-Host cmdlet, and store it in a variable called $url, as shown below:

$url=Read-Host -Prompt 'Input a Right Move URL and press enter'

You’ll then see the below, where you can paste your URL in, and press enter:

Download the HTML source code of the website

To download the HTML source code of the listing, we’ll use Invoke-WebRequest, but first, we need to set the TLS protocols that we want to use. You may not need this, but depending on your version of PowerShell, you might, so lets include it:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

Then, let’s download the data via an Invoke-WebRequest, and save it to a variable called $html

$webreq = Invoke-WebRequest $url
$html=$webreq.Content

Folder structure

Now we have the HTML source code, let’s think about how to organise the downloaded photo, and for this I believe it’s best to create sub folders. For this example, I’ll use the title of the listing, which is withing the <title></title> tags of the pages HTML.

There are various ways to get the content between two strings, or HTML tags in this case, but I like to do it via a function. For this example, we’ll pass in the opening tag/string (<title>), closing tag/string (</title>) and the html source code.

Function:

function GetStringBetweenTwoStrings($openingtag, $closingtag, $html){

    #Regex pattern to compare two strings
    $pattern = "$openingtag(.*?)$closingtag"

    #Perform the opperation
    $result = [regex]::Match($html,$pattern).Groups[1].Value

    #Return result
    return $result

}

Getting the title from the HTML source code:

$title=GetStringBetweenTwoStrings -openingtag "<title>" -closingtag "</title>" -html $html

We’ll take the new $title variable, and prepend a date string to it, just in case you choose to re-download the same listing again:

$foldername="$((Get-Date).ToString('yyyyMMdd'))_$($title -replace " ","_")"

To create the folder, first we need to check if it’s already there, although this is unlikely since we have a date string, but it is possible, and if not, create it:

$saveto=".\RIGHTMOVE\$($foldername)"

[System.IO.Directory]::Exists($saveto)
if (!(Test-Path $saveto)) {
  Write-Warning "$saveto does not exist, it will be created now"
  New-Item -Path $saveto -ItemType Directory
}

Finding the images from the HTML code

To find all of the images in the HTML source code of the Rightmove listing, we need to find all of the meta tags with the og:image property. The easiest way to do this is with regex, and we’ll filter those into a different variable called $metatags

$metatags = ([regex]'<meta property="og:image" ((.|\n|\r)+?)\/>').Matches($html)

Saving the images with PowerShell

The Rightmove photos wont necessarily be named in a friendly way, so as we save them, we’ll append a numeric value, so they are in order.

We’ll start by looping through each of the already filtered og:image meta tags using ForEach, and for each individual item, we’ll strip out what we don’t need to get the URL and lastly download the image. We could have used the function to return the URL, but I’ve done this way to show you another approach, even though it’s not normally what I would do, but it does work:

[int]$int=1
ForEach ($metatag in $metatags)
{
    $imageurl=(($metatag -replace "`"/>","") -replace "<meta property=`"og:image`" content=`"","")
    Write-Host $imageurl

    Invoke-WebRequest -Uri $imageurl -OutFile "$($saveto)`\Image_$('{0:d2}' -f [int]$int).jpg" -TimeoutSec 1500
    $int++
}

The full PowerShell script for downloading Rightmove listing photos

Save this text as in a text file, then make sure you rename it so the extension is .ps1 instead of .txt

CLEAR

$url=Read-Host -Prompt 'Input a Right Move URL and press enter'

function GetStringBetweenTwoStrings($openingtag, $closingtag, $html)
{
    $pattern = "$openingtag(.*?)$closingtag"
    $result = [regex]::Match($html,$pattern).Groups[1].Value
    return $result
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$webreq = Invoke-WebRequest $url
$html=$webreq.Content #| Out-File C:\temp\output.txt

$title=GetStringBetweenTwoStrings -openingtag "<title>" -closingtag "</title>" -html $html
$foldername="$((Get-Date).ToString('yyyyMMdd'))_$($title -replace " ","_")"

Write-Host $foldername

$saveto=".\RIGHTMOVE\$($foldername)"

[System.IO.Directory]::Exists($saveto)
if (!(Test-Path $saveto)) {
  Write-Warning "$saveto does not exist, it will be created now"
  New-Item -Path $saveto -ItemType Directory
}

Write-Host "Title: $title"
Write-Host "URL: $url"

$metatags = ([regex]'<meta property="og:image" ((.|\n|\r)+?)\/>').Matches($html)

[int]$int=1
ForEach ($metatag in $metatags)
{
    $imageurl=(($metatag -replace "`"/>","") -replace "<meta property=`"og:image`" content=`"","")
    Write-Host $imageurl

    Invoke-WebRequest -Uri $imageurl -OutFile "$($saveto)`\Image_$('{0:d2}' -f [int]$int).jpg" -TimeoutSec 1500
    $int++
}

When run, you will see an output similar to this:

The photos will be saved in a photo like this:

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.