4

いくつかの画像ギャラリーをまとめてダウンロードしたいと思います。画像は、許可を必要とせずに無料で提供されます。私は一生、それを機能させることができません。これは私がこれまでに持っているものです。$pattern が吐き出すのは、画像リンクだけでなく、HTML 行全体です。あなたが私に与えることができる指針はありますか?ループは、テスト目的で 1 回だけ実行するように設定されています。ループは、数値的に編成されたすべてのページを通過します。

# 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

2 に答える 2

3

一致した値を抽出する必要があります。Select-Stringはオブジェクトを返します。そのときecho、何が起こるかは$pattern.ToString(). ToString()match-value ではなく、行を返します。これにより、すべてのリンクのみが返されます。

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

ところで、Web ページを保存して で再度開くのではなく、get-content単純に文字列を改行で分割して配列を取得できます (それが保存した唯一の理由である場合)。:-)

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

EDITダウンロードするには、別の foreach ループで拡張するだけです。

$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)
}
于 2013-04-07T08:57:49.800 に答える
0

Select-Stringプロパティを持つオブジェクトを返します。に送信して、Get-Memberあなたが持っているグッズを確認してください。たとえば、matches プロパティを確認する必要があります$pattern.matchesドキュメントの例 9 を確認してください。

于 2013-04-07T07:40:47.650 に答える