Powershell - 从图像网址下载图像

时间:2014-03-17 05:04:52

标签: powershell webclient

对PowerShell的知识有限。
我尝试从图片网址下载图片。例如像这样: " http://hdwallpaperia.com/wp-content/uploads/2014/01/Mc-Laren-P1-Wallpaper-Image-Picture-640x360.jpg"

在到达链接之前,我必须先登录。这是我的登录页面HTML代码:

<tr>
    <td colspan="2">Please enter your user name and password below:</td>
</tr>
<tr style="height: 10px;"><td></td></tr>
<tr>
    <td>User Name:</td>
    <td><input name="login_username" style="width: 295px;" type="text" size="40" value=""></td>
</tr>
<tr>
    <td>Password:</td>
    <td><input name="login_password" style="width: 295px;" type="password" size="40"></td>
</tr>
<tr>
    <td>Realm:</td>
    <td>
        <select name="realm" style="width: 295px;"><option value="local">Local</option><option value="ldap" selected="">LDAP</option>
        </select>
    </td>
</tr>
    <tr style="height: 10px;"><td></td></tr>
<tr>
    <td><input type="submit" value="Login"></td>
</tr>   

这是我的powershell代码:

$url = "http://local_machine/example_640x360.jpg"

$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($url)

$ie.Document.getElementByid("login_username").value = "$Account"
$ie.Document.getElementByid("login_password").value = "$Password"
$ie.Document.getElementByid("realm").value = "LDAP"

$Log_In=$ie.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"}
$Log_In.click();

while($ie.Busy) {Start-Sleep -s 1}

#$ie.Document.Body | Out-File -FilePath "c:\temp\test.jpg"
$ie.quit()

我可以成功登录并访问img链接,但不知道如何下载图片。什么命令可以帮助我下载?

6 个答案:

答案 0 :(得分:11)

你使用COM过度复杂化了。我无法测试这些atm。但它们应该可以工作。

#Solution 1 - WebClient
$url = "http://www.united.no/wp-content/uploads/2014/03/moyesliver.jpg"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, "C:\temp\test.jpg")


#Solution 2 - never tried this before
Invoke-WebRequest $url -OutFile C:\temp\test.jpg

答案 1 :(得分:2)

我会使用iwr和位传输并使其变得简单。我继续为你做了多个文件,这比你刚抓住一个文件更有可能。 #automateallthethings。

如果您想下载多个文件,可以试试这个:

$URL = "http://www.website.com"

$Site = iwr -Uri $URL

$Images = ($Site).Images.src

foreach ($Image in $Images) {

Start-BitsTransfer -Source $Image -Destination C:\Test\ -TransferType Download

}

答案 2 :(得分:2)

这对我有用;)

$wc = New-Object System.Net.WebClient
$wc.DownloadFile("https://asdfasdfxxx.blob.core.windows.net/images/test.png", "C:\test.png")

Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value "C:\test.png"

rundll32.exe user32.dll, UpdatePerUserSystemParameters

kill -n explorer

答案 3 :(得分:0)

这是用于下载所有图像(我知道有16张)

$url="http://www.MySite.tn/images/symboles/"
for ($i=1; $i -lt 16;$i++) {
    Invoke-WebRequest $url/$i.png -OutFile C:\Users\MyUser\Documents\icons\$i.png
    }

答案 4 :(得分:0)

试试看,这不是一个完美的脚本,但是只要您在URI的末尾提供一个“ /”,它就可以工作。某些图片存储在与您可能请求的URI略有不同的路径上,因此,例如,“ google.com/”无法正常工作,但使URI变量“ www.google.com/”解决了该问题。

$path = $null
$filter = $null

$uri = 'www.google.com/'
Write-Host "URI: $uri" -ForegroundColor Green

$filter = $uri | Select-String -AllMatches -Pattern '(\w.+)\/'
$path = $filter.Matches.Groups[1].Value
Write-Host "Server: $path" -ForegroundColor Yellow

$site = Invoke-WebRequest -Uri $uri

$images = $site.Images.src

foreach ($image in $images) {

    Write-Host "Files that are being downloaded:" -BackgroundColor Black -ForegroundColor White
    Write-Host $image -ForegroundColor DarkYellow
    Start-BitsTransfer -Source "http://$path$image" -Destination '.\TEMP\' -TransferType Download

                            }

答案 5 :(得分:0)

`$path = $null $filter = $null

$uri = 'www.google.com/'
Write-Host "URI: $uri" -ForegroundColor Green

$filter = $uri | Select-String -AllMatches -Pattern '(\w.+)\/'
$path = $filter.Matches.Groups[1].Value
Write-Host "Server: $path" -ForegroundColor Yellow

$site = for ($i=1; $i -lt 2;$i++) {Invoke-WebRequest -Uri $uri -ContentType Images -UseBasicParsing -DisableKeepAlive }

$images = $site.Images.src
$images

#again,这只会给你 url 路径,而不是文件...

相关问题