Web Scraping with PowerShell

This post will show how to use PowerShell (PS) to scrap data from the website. In this particular case, we will scrap information from the (water) utility company in Montenegro. After we harvest the necessary data (amount value), it will be written into dedicated CSV file.

Defining the variable

Date value:
$date = Get-Date

Getting the HTML content of the webpage:
$export = Invoke-WebRequest -Uri https://vikpg.me/me/plati-fizicko-lice?sifra=217576700/1

Extracting desired part of the text (value) from the HTML, using RegEx:
$value = [regex]::Match($export, '[^name="TotalAmount" value="]+(?=" class=)').Value

    RegEx is a sequence of characters that specifies a match pattern in text. Good starting point: https://regex101.com/

    Write information into CSV file:

    "{0},{1},{2}" -f $export, $date, $value | add-content -path C:\PowerShellScraper\processes.csv

    Potential issue might appear if extracted value (number) contains “,”. That will cause CSV file wrongly to read.

    Scroll to Top