Take links to an image from an HTML website using powershell

I want to upload several image galleries in bulk. Images are offered free of charge without any permission. For my life I can’t make it work. This is what I still have. The expression $ pattern is a whole line of HTML, not just a link to an image. Are there any pointers you can give me? The cycle is set to run only once for testing purposes. The cycle will go through all the pages that are organized numerically.

# Variables $i=1 # Webpage Counter $j=1 # Image Counter $rootDir = "http://website.com/sport/galleries/" $saveDir = "C:\Users\user\Desktop\" $webpagetxt = "C:\Users\user\Desktop\page.txt" $links = "C:\Users\user\Desktop\links.txt" $regex = "http://website.com/galleries/[0-9]*/[^\.]*.JPG" # Create folder to download to #New-Item -Name SiouxSportsGalleries -ItemType directory # Start Web Client $client = New-Object System.Net.WebClient # Main loop to get image links and download For($i=10; $i -le 10; $i++){ # Download source code of the web page. $url = $rootDir+$i+'.htm' $webclient = new-object System.Net.WebClient $webpage = $webclient.DownloadString($url) $webpage > "$webpagetxt" # Parse web page and find image link. $pattern = Get-Content $webpagetxt | Select-String -pattern $regex -Allmatches echo "This is the link" $pattern #$pattern > $links } 
+4
source share
2 answers

You need to extract the value that was a match. Select-String returns objects, and when you echo what happens $pattern.ToString() . ToString() returns a string, not a match value. This will return only all links:

 Get-Content $webpagetxt | Select-String -pattern $regex -Allmatches | % { $_.Matches | % { $_.Value } } 

Btw, instead of saving the web page and reopening it with get-content , you can simply split the string into linebreaks to get an array (if that was the only reason you saved it) :-)

 $webpage -split "`n" | Select-String -pattern $regex -Allmatches | % { $_.Matches | % { $_.Value } } 

EDIT To load it, you can simply extend it with another foreach loop:

 $rootDir = "http://website.com/sport/galleries/" $saveDir = "C:\Users\user\Desktop\" $webpage -split "`n" | Select-String -pattern $regex -Allmatches | % { $_.Matches | % { $_.Value } } | % { #Get local path $local = $_.Replace($rootDir, $saveDir) #Create path $file = New-Item $local -ItemType file -Force #Download $wb.DownloadFile($_, $file.FullName) } 
+3
source

Select-String returns you an object with properties. Send it to Get-Member to find out which ones you have. You will want to check the match property, for example. $pattern.matches . Example 9 is given in the documentation .

0
source

All Articles