从网站下载文件的最新版本/从按钮获取最新的下载链接(Windows 10)

huangapple go评论83阅读模式
英文:

Downloading latest version of file from website / Getting latest download Link from button (Windows 10)

问题

我需要下载一堆文件的最新版本。使用直接链接非常简单。

我需要使用的链接看起来像这样:https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest
这是下载按钮的链接,它每次都会生成一个新的直接下载链接。

在Web浏览器中,此链接将下载最新版本的Telegram .apk。但由于直接下载链接会更改,我无法在我的脚本中使用它,而按钮链接将导致一个HTML文件。

我尝试过使用curl、Invoke-WebRequest和DownloadFile()。在Web浏览器中使用直接下载链接时,它可以无缝运行。我想要使用脚本生成此下载链接。

如何使用此下载按钮来始终获取最新文件/最新下载链接?

英文:

I need to download the newest version of a bunch of files. That works pretty straight forward using direct links.

The links I need to use look something like this: https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest
It's the link of the download button, which generates a new direct download link every time.

In the web browser this link will download the latest Telegram .apk. I can not use the direct download link in my script since it will change, and the link to the button will result in a html file.

I tried it using curl, Invoke-Webrequest and DownloadFile(). It works seamlessly using the direct download links generated when using a web browser. I'd like to generate this download link using a script.

How do I use this download button to always get the latest file/latest download link?

答案1

得分: 0

要获取最终重定向的 URL,请使用以下脚本:

$URL = "https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest"
$request = [System.Net.WebRequest]::Create($URL)
$request.AllowAutoRedirect = $true
$request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6' #有助于处理复杂页面...

try {
    $response = $request.GetResponse()
    $redirectedURL = $response.ResponseUri.AbsoluteUri
    $response.Close()
} catch {
    "Error: $_"
}

然后,您可以通过 Invoke-WebRequest 下载:

Invoke-WebRequest -URI $redirectedURL -OutFile ".\a.apk" -user
英文:

To get final redirected url use this script:

$URL= "https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest"
$request = [System.Net.WebRequest]::Create($URL)
$request.AllowAutoRedirect=$true
$request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6' #helps with difficult pages...

try{
    $response = $request.GetResponse()
    $redirectedURL = $response.ResponseUri.AbsoluteUri
    $response.Close()
} catch {
    "Error: $_"
}

Then you can download via Invoke-WebRequest

Invoke-WebRequest -URI $redirectedURL -OutFile ".\a.apk" -user

答案2

得分: 0

$url = "https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest"
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect = $true
$request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6' #helps with difficult pages...

try {
    $response = $request.GetResponse()
    $redirectedURL = $response.ResponseUri.AbsoluteUri

    $dispositionHeader = $Response.Headers['Content-Disposition']
    $disposition = [System.Net.Mime.ContentDisposition]::new($dispositionHeader)

    $fileName = $disposition.FileName
    $response.Close()
} catch {
    "Error: $_"
}
Invoke-WebRequest -URI $redirectedURL -OutFile ".\APKs$fileName"
英文:

My Code:

            $url= "https://d.apkpure.com/b/APK/org.telegram.messenger?version=latest"
            $request = [System.Net.WebRequest]::Create($url)
			$request.AllowAutoRedirect=$true
			$request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6' #helps with difficult pages...

			try{
				$response = $request.GetResponse()
				$redirectedURL = $response.ResponseUri.AbsoluteUri
				
				$dispositionHeader = $Response.Headers['Content-Disposition']
				$disposition = [System.Net.Mime.ContentDisposition]::new($dispositionHeader)
				
				$fileName = $disposition.FileName
				$response.Close()
			} catch {
				"Error: $_"
			}
			Invoke-WebRequest -URI $redirectedURL -OutFile ".\APKs$fileName"
    

</details>



huangapple
  • 本文由 发表于 2023年2月27日 16:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定