如何在代理机器中找到下载的文件路径?
请尝试以下 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 脚本中的目标路径。
这是一篇关于从 url 使用 Python 下载文件的博客
更新:
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)