IT automation and programming tips

Jidouka.work

Windows

Make an image downloader [PowerShell]

投稿日:

PowerShell - a tool bundled on Windows has rich functions to extract elements in HTML pages easily.
I made a script to download images in a HTML page.

Code

Get arguments on PowerShell script

In order to get arguments on PowerShell, "Param(arg1, arg2, ...)" is used.
At this time, The script receives 1 URL.

Param( $url )

Get a HTML page

“Invoke-WebRequest” is used to get HTML page by a PowerShell script.
An object of the page is set to $page.

$page = Invoke-WebRequest $url

The page got by the script has links. The script need to check if the each link is for an image or not.
You can check each variable by foreach($variable in $array).

foreach($linkElement in $page.Links.href)

Escape from the loop after checking all the links.

In order to copy file name from the link, the script has to copy the last element of the path string.
“Split-Path” is used to do it. “Split-Path” has a option to get the last element(Leaf).

$fileName = Split-Path $linkElement -Leaf

More information is available on MSDN.

Split-Path
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-5.1

Get the extension from the file name

Path class in System.IO namespace is used to get the extension from the file name.

Path Class (System.IO)
https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx

[IO.Path]::GetExtension($string)

This Path class can not only getting extension but also do some operation to the string of the path.

Recognize whether the extension is image or not

If statement can be used to judge the extension. I thought that PowerShell has a function to classify extension type, but there is no such function.

if ( $extension -eq ".jpg" -or $extension -eq ".png”)

Downloading images

Invoke-WebRequest is used to download images.

Invoke-WebRequest $linkElement -OutFile $fileName

Images must be exported as file. Therefore, -OutFile option is used and the file name is used as an argument.

Save the PowerShell script and Run

PowerShell script file has “.ps1” extension.

I downloaded images as a trial.
Add “.\” at the head of the command to execute the script.

PowerShell command download images

As the result, images were downloaded and saved.

Images download result


-Windows
-

Copyright© Jidouka.work , 2024 All Rights Reserved.