【问题标题】:Download file from a URL in Azure Pipelines Microsoft hosted agent从 Azure Pipelines Microsoft 托管代理中的 URL 下载文件
【发布时间】:2021-04-01 07:19:06
【问题描述】:

我正在 Azure YAML 管道中运行 Python 脚本任务。通过浏览器访问 URL 时会下载 JSON 文件。 网址 - 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 下载文件而无需实际打开浏览器?

【问题讨论】:

    标签: python azure-pipelines python-webbrowser azure-pipelines-yaml azure-pipelines-tasks


    【解决方案1】:

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

    请尝试以下 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的博客

    更新:

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

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

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

    例如:网址: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:

    另一种获取下载地址的方法:

    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)
         
    

    【讨论】:

    • 能否请您简要解释一下您在 update3 中做了什么?
    • 第三次更新,直接通过request获取页面源码(html),然后使用正则表达式直接获取匹配的url。这可能是一种更简单的方法。
    • 哦,好的。那行->“ a = t.group() ”怎么样。那有什么作用?
    • 这用于返回匹配的对象。并将值传递给一个
    猜你喜欢
    • 2021-01-05
    • 2021-03-09
    • 2018-10-29
    • 2021-04-14
    • 2019-02-20
    • 2019-02-21
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多