如何从PowerShell网站上获取链接的价值?

发布于 2025-02-08 21:24:19 字数 398 浏览 3 评论 0原文

我想从其网站上获得最后一个版本的下载url,我编写了一个脚本,但是它返回了链接名称,我不知道如何获得

$web = Invoke-WebRequest -Uri "https://download.gimp.org/pub/gimp/v2.10/windows/"
$web.Links | Where-Object href -like '*exe' | select -Last 1 | select -expand href  

上述代码返回链接名称(GIMP-2.10.32-setup)。 EXE文件) 但是我需要值(“ https://download.gimp.org/pub/gimp/v2.10/windows/windows/gimp-2.10.32-setup.exe”) 有人可以指导我如何做吗

I want to get download URL of the last version of GIMP from it's site ,I wrote a script but it returns the link name I do not know how to get the value

$web = Invoke-WebRequest -Uri "https://download.gimp.org/pub/gimp/v2.10/windows/"
$web.Links | Where-Object href -like '*exe' | select -Last 1 | select -expand href  

the above code returne link name (gimp-2.10.32-setup.exe)
but I need the value ("https://download.gimp.org/pub/gimp/v2.10/windows/gimp-2.10.32-setup.exe")
can someone guide me how to do it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2025-02-15 21:24:19

您知道呈现的URL是相对的。
只需亲自附加URL的根部。

  $Uri = 'https://download.gimp.org/pub/gimp/v2.10/windows/'
  $web = Invoke-WebRequest -Uri $uri
  $ExeRelLink =  $web.Links | Where-Object href -like '*exe' | select -Last 1 -expand href 
# Here is your download link.
$DownloadLink = $Uri + $ExeRelLink 

附加说明

您可以将last-expand从2个选择语句中组合为1。

You know that the url presented is relative.
Just append the root part of the URL yourself.

  $Uri = 'https://download.gimp.org/pub/gimp/v2.10/windows/'
  $web = Invoke-WebRequest -Uri $uri
  $ExeRelLink =  $web.Links | Where-Object href -like '*exe' | select -Last 1 -expand href 
# Here is your download link.
$DownloadLink = $Uri + $ExeRelLink 

Additional Note

You can combine the -Last and -Expand from your 2 select statements into 1.

如此安好 2025-02-15 21:24:19

有几个下载网站与此GIMP页面完全相同或非常相似的布局,包括许多Apache项目,例如 tomcat activemq 。过去,我写了一些功能来解析这些和其他页面,有趣的是,它也适用于此GIMP页面。我认为值得分享。

Function Extract-FilenameFromWebsite {
    [cmdletbinding()]
    Param(
        [parameter(Position=0,ValueFromPipeline)]
        $Url
    )

    begin{
        $pattern = '<a href.+">(?<FileName>.+?\..+?)</a>\s+(?<Date>\d+-.+?)\s{2,}(?<Size>\d+\w)?'
    }

    process{
        $website = Invoke-WebRequest $Url -UseBasicParsing

        switch -Regex ($website.Content -split '\r?\n'){
            $pattern {
                [PSCustomObject]@{
                    FileName     = $matches.FileName
                    URL          = '{0}{1}' -f $Url,$matches.FileName
                    LastModified = [datetime]$matches.Date
                    Size         = $matches.Size
                }
            }
        }
    }
}

假设该地点通过了一个落后的斜线。如果要考虑任何一个,则可以将此简单的行添加到过程块中。

if($Url -notmatch '/

要获取最新版本,请这样调用此功能

$url = 'https://download.gimp.org/pub/gimp/v2.10/windows/'

$latest = Extract-FilenameFromWebsite -Url $Url | Where-Object filename -like '*exe' |
    Sort-Object LastModified | Select-Object -Last 1

$latest.url

,或者您可以在检索时扩展属性

$url = 'https://download.gimp.org/pub/gimp/v2.10/windows/'

$latesturl = Extract-FilenameFromWebsite -Url $Url | Where-Object filename -like '*exe' |
    Sort-Object LastModified | Select-Object -Last 1 -ExpandProperty URL

$latesturl
){$Url = "$Url/"}

要获取最新版本,请这样调用此功能

,或者您可以在检索时扩展属性

There are several downloads sites with exactly the same or very similar layout to this GIMP page, including many Apache projects like Tomcat and ActiveMQ. I had written a little function to parse these and other pages in the past, and interestingly it also worked for this GIMP page. I thought it was worth sharing as such.

Function Extract-FilenameFromWebsite {
    [cmdletbinding()]
    Param(
        [parameter(Position=0,ValueFromPipeline)]
        $Url
    )

    begin{
        $pattern = '<a href.+">(?<FileName>.+?\..+?)</a>\s+(?<Date>\d+-.+?)\s{2,}(?<Size>\d+\w)?'
    }

    process{
        $website = Invoke-WebRequest $Url -UseBasicParsing

        switch -Regex ($website.Content -split '\r?\n'){
            $pattern {
                [PSCustomObject]@{
                    FileName     = $matches.FileName
                    URL          = '{0}{1}' -f $Url,$matches.FileName
                    LastModified = [datetime]$matches.Date
                    Size         = $matches.Size
                }
            }
        }
    }
}

It's assumed the site passed in has a trailing slash. If you want to account for either, you can add this simple line to the process block.

if($Url -notmatch '/

To get the latest version, call the function like this

$url = 'https://download.gimp.org/pub/gimp/v2.10/windows/'

$latest = Extract-FilenameFromWebsite -Url $Url | Where-Object filename -like '*exe' |
    Sort-Object LastModified | Select-Object -Last 1

$latest.url

Or you could expand the property while retrieving

$url = 'https://download.gimp.org/pub/gimp/v2.10/windows/'

$latesturl = Extract-FilenameFromWebsite -Url $Url | Where-Object filename -like '*exe' |
    Sort-Object LastModified | Select-Object -Last 1 -ExpandProperty URL

$latesturl
){$Url = "$Url/"}

To get the latest version, call the function like this

Or you could expand the property while retrieving

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文