gpt4 book ai didi

html - 使用 powershell 从 HTML 网站抓取图像链接

转载 作者:搜寻专家 更新时间:2023-10-31 08:34:14 25 4
gpt4 key购买 nike

我想批量下载一些图库。这些图像是免费提供的,不需要任何权限。我终生无法让它发挥作用。这是我到目前为止所拥有的。吐出的 $pattern 是整个 HTML 行,而不仅仅是图像链接。你有什么指示可以给我吗?出于测试目的,循环被设置为只运行一次。该循环将遍历所有按数字组织的页面。

# 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

}

最佳答案

您需要提取匹配的值。 Select-String 返回对象,当您 echo 它时,发生的是 $pattern.ToString()ToString() 返回行,而不是匹配值。这将只返回所有链接:

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

顺便说一句,不是保存网页并使用 get-content 重新打开它,您可以简单地在换行符处拆分字符串以获取数组(如果这是您保存它的唯一原因)。 :-)

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

编辑 要下载它,您可以用另一个 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)
}

关于html - 使用 powershell 从 HTML 网站抓取图像链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15859843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com