[英]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 下載文件而無需實際打開瀏覽器?
如何在代理機器中找到下載的文件路徑?
請嘗試以下 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 腳本中的目標路徑。
這是一篇關於使用 Python 從 url 下載文件的博客
更新:
url: microsoft.com/en-us/download/confirmation.aspx?id=56519
需要打開 web 頁面,文件會自動下載。
所以當你使用 wget 或 urllib.request 時,你會得到 403 錯誤。
您可以更改為使用站點 url 手動下載 json 文件。
For example: 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.