从 Azure Pipelines Microsoft 托管代理中的 URL 下载文件

Download file from a URL in Azure Pipelines Microsoft hosted agent

我是 运行 Azure YAML 管道中的 Python 脚本任务。当通过浏览器访问 URL 时,会下载一个 JSON 文件。 URL - https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519

到目前为止我做了什么 -->

- task: PythonScript@0
  name: pythonTask
  inputs:
    scriptSource: 'inline'
    script: |

      url = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519"

      import webbrowser
      webbrowser.open(url)
      print("The web browser opened and the file is downloaded")

一旦浏览器打开 URL,文件就会自动下载到本地。 但是,在 运行 上述管道上,我似乎无法在代理机器上的任何地方找到该文件。我也没有收到任何错误。

我正在使用 Windows-2019 Microsoft 托管代理。

如何在代理机器中找到下载的文件路径?

或者是否有另一种方法可以让我从 URL 下载文件而无需实际打开浏览器?

How can I find the downloaded file-path inside the agent machine?

请尝试以下 Python 脚本:

steps:
- task: PythonScript@0
  displayName: 'Run a Python script'
  inputs:
    scriptSource: inline
    script: |
     import urllib.request
     

     
     url = 'https://www.some_url.com/downloads'
     
     path = r"$(Build.ArtifactStagingDirectory)/filename.xx"
     urllib.request.urlretrieve(url, path)

steps:
- script: 'pip install wget'
  displayName: 'Command Line Script'

- task: PythonScript@0
  displayName: 'Run a Python script'
  inputs:
    scriptSource: inline
    script: |
     import wget
     
     print('Beginning file download with wget module')
     
     url = 'https://www.some_url.com/downloads'
     path = r"$(Build.ArtifactStagingDirectory)"
     wget.download(url, path)

然后文件将被下载到Python脚本中的目标路径。

这是一篇关于 use Python download files from url

的博客

更新:

url:microsoft.com/en-us/download/confirmation.aspx?id=56519需要打开网页自动下载文件

所以当你使用 wget 或 urllib.request 时,你会得到 403 错误。

您可以更改为使用站点 url 手动下载 json 文件。

例如:url:https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20210329.json

import urllib.request

url = 'https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20210329.json'

path = r"$(Build.ArtifactStagingDirectory)\agent.json"
urllib.request.urlretrieve(url, path)

更新2:

您可以使用 Python 脚本在网站上下载。

样本:

steps:
- script: |
   pip install bs4
   
   pip install lxml
  workingDirectory: '$(build.sourcesdirectory)'
  displayName: 'Command Line Script'

- task: PythonScript@0
  displayName: 'Run a Python script'
  inputs:
    scriptSource: inline
    script: |
     from bs4 import BeautifulSoup
     from urllib.request import Request, urlopen
     import re
     import urllib.request
     
     
     req = Request("https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519" , headers={'User-Agent': 'Mozilla/5.0'})
     html_page = urlopen(req).read()
     
     a=""
     soup = BeautifulSoup(html_page, "lxml")
     
     for link in soup.find_all('a' , id="c50ef285-c6ea-c240-3cc4-6c9d27067d6c"):
         
          a= link.get('href')
          print(a)
     
     
     
     path = r"$(Build.sourcesdirectory)\agent.json"
     urllib.request.urlretrieve(a, path)

结果:

更新3:

获取下载的另一种方法URL:

steps:
- script: 'pip install requests'
  displayName: 'Command Line Script'

- task: PythonScript@0
  displayName: 'Run a Python script'
  inputs:
    scriptSource: inline
    script: |
     import requests
     import re
     import urllib.request
     
     rq= requests.get("https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519")
      
     t = re.search("https://download.microsoft.com/download/.*?\.json", rq.text )
      
     
     
     a= t.group()
     
     print(a)
     
     path = r"$(Build.sourcesdirectory)\agent.json"
     urllib.request.urlretrieve(a, path)