使用PowerShell从HTML网站获取图像链接
发布时间:2020-12-14 21:18:08 所属栏目:资源 来源:网络整理
导读:我想批量下载一些图片库.这些图像是免费提供的,无需任何权限.我为我的生活无法让它发挥作用.这就是我到目前为止所拥有的. $pattern吐出的是整个HTML行,而不仅仅是图像链接.你有什么指示可以给我吗?循环设置为仅运行一次以进行测试.循环将遍历所有以数字方式
我想批量下载一些图片库.这些图像是免费提供的,无需任何权限.我为我的生活无法让它发挥作用.这就是我到目前为止所拥有的. $pattern吐出的是整个HTML行,而不仅仅是图像链接.你有什么指示可以给我吗?循环设置为仅运行一次以进行测试.循环将遍历所有以数字方式组织的页面.
# Variables $i=1 # Webpage Counter $j=1 # Image Counter $rootDir = "http://website.com/sport/galleries/" $saveDir = "C:UsersuserDesktop" $webpagetxt = "C:UsersuserDesktoppage.txt" $links = "C:UsersuserDesktoplinks.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返回对象,当你回显它时,发生的是$pattern.ToString(). ToString()返回行,而不是匹配值.这将仅返回所有链接:
Get-Content $webpagetxt | Select-String -pattern $regex -Allmatches | % { $_.Matches | % { $_.Value } } 顺便说一句,你可以简单地在换行符上拆分字符串来获取一个数组(如果这是你保存它的唯一原因),而不是保存网页并用get-content重新打开它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |