SearchYahooImagesDownload.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID cf1173f4-5876-4ca8-a348-20d2df75b55a
 
.AUTHOR mikko@lavento.com
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Yahoo, imagesearch, download image, image, search
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Example how to download first x number image hits from Yahoo image search. This can be done without Yahoo APIs because it makes direct request mimicking browser request.
 
#>
 

Param()


#19.7.2019 M.Lavento
#Get first x number of images from Yahoo image search based on Searchterm

Add-Type -AssemblyName System.Web

$SearchPlaintext = "LPS #7"
#Convert searchstring to HTTP
$SearchItem = [System.Web.HttpUtility]::UrlEncode($SearchPlaintext)

$HowManyHits = "4"

#Folder to store pics
$TargetFolder = "C:\Skriptit\WebImageCrawler\Downloadedpics"
if ( (Test-Path -Path $TargetFolder) -eq $false) { md $TargetFolder }
Invoke-Item $TargetFolder

$url = "https://images.search.yahoo.com/search/images;?p=$SearchItem"

$browserAgent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36'
$page = Invoke-WebRequest -Uri $url -UserAgent $browserAgent -UseBasicParsing

#Yahoo, have to read hits from rawcontent
$jpgsplit = $page.RawContent -split "," | where {$_ -like "*.jpg*"}
$jpgonlysplit = $jpgsplit | where {$_ -like "*iurl*"} 

#clean up the answer some more and get 10 first results
$iurlsplit = $jpgonlysplit -split '":"' -replace '"', "" -replace "[\\]", ""| where {$_ -like "*.jpg*"} | select -First $HowManyHits

#Loop and get the images
foreach ($urlhit in $iurlsplit)
{

#Save file
$file = Split-Path -Path $urlhit -Leaf
$finalfile = $SearchPlaintext + "_" + $file
#replace illegal chars if there is any
[System.IO.Path]::GetInvalidFileNameChars() | foreach {$finalfile = $finalfile.replace($_,' ')}
$path = Join-Path -Path $TargetFolder -ChildPath $finalfile
#fetch the image
Invoke-WebRequest -Uri $urlhit -OutFile $path -ErrorAction Continue

}