从网站下载图像

时间:2016-03-05 20:44:36

标签: powershell

我运行了这个powershell脚本从网站下载图像(下载它,必须执行某些步骤,这就是我使用IE导航的原因)。我把一个随机字符串放在4到4个字符之间。

但是我收到一个错误,它甚至没有开始用字符串填充空白:

Exception from HRESULT: 0x800A01B6
At E:\getbd.ps1:13 char:1
+ $ie.Document.getElementsByTagName("text") | where { $.name -eq "words ...

以下是代码:

$url = "https://fakecaptcha.com"
$set = "abcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()
for($i=1; $i -le 4; $i++){
$result += $set | Get-Random}
$result += ' '
for($i=1; $i -le 4; $i++){
$result += $set | Get-Random}
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1}
$ie.Document.getElementsByTagName("text") | where { $.name -eq "words" }.value = $result
$generateBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'submit' -and $_.Value -eq 'Create it now!'} 
$generateBtn.click() 
while( $ie.busy){Start-Sleep 1}
$readyBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'Your captcha is done! Please click here to view it!!'} 
$readyBtn.click() 
while( $wc.busy){Start-Sleep 1}
$downloadBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'DOWNLOAD'} 
$downloadBtn.click()
while( $ie.busy){Start-Sleep 1}
$source = $ie.document.getElementsByTagName('img') | Select-Object -ExpandProperty src  
$file = '$E:\bdlic\'+$result+'.jpg'
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source,$file)
while( $wc.busy){Start-Sleep 1}
$ie.quit()

1 个答案:

答案 0 :(得分:1)

该行中有2个语法错误:

$ie.Document.getElementsByTagName("text") | where { $.name -eq "words" }.value = $result
#                                                   ^                   ^^^^^^
  1. $.Name:“当前对象”变量为$_,而不仅仅是$
  2. where {...}.value:您不能在Where-Object语句的scriptblock上使用点符号。您需要将整个语句放在(子)表达式中。
  3. 将行更改为:

    ($ie.Document.getElementsByTagName("text") | where { $_.name -eq "words" }).value = $result
    
相关问题