我想从网页下载一个文件。该网页只有一个.zip文件(这就是我想要下载的),但当我点击.zip文件时,它开始下载,但URL没有改变(URL仍然是表单中的URLhttp://ldn2800:8080/id=2800)。考虑到没有http://example.com/1.zip
形式的URL,我如何使用python下载它?
此外,当我直接转到页面时http://ldn2800:8080/id=2800,它只是用.zip文件打开该页面,但不点击就不会下载。如何使用python下载它?
更新:现在我是这样做的:
if (str(dict.get('id')) == winID):
#or str(dict.get('id')) == linuxID):
#if str(dict.get('number')) == buildNo:
buildTypeId = dict.get('id')
ID = dict.get('id')
downloadURL = "http://example:8080/viewType.html?buildId=26009&tab=artifacts&buildTypeId=" + ID
directory = BindingsDest + "\" + buildNo
if not os.path.exists(directory):
os.makedirs(directory)
fileName = None
if buildTypeId == linuxID:
fileName = linuxLib + "-" + buildNo + ".zip"
elif buildTypeId == winID:
fileName = winLib + "-" + buildNo + ".zip"
if fileName is not None:
print(dict)
downloadFile(downloadURL, directory, fileName)
def downloadFile(downloadURL, directory, fileName, user=user, password=password):
if user is not None and password is not None:
request = requests.get(downloadURL, stream=True, auth=(user, password))
else:
request = requests.get(downloadURL, stream=True)
with open(directory + "\" + fileName, 'wb') as handle:
for block in request.iter_content(1024):
if not block:
break
handle.write(block)
但是,它只是在所需的位置创建了一个zip,但该zip无法打开,并且什么都没有。可以这样做吗:比如在网页上搜索文件名,然后下载匹配的模式?
检查HTTP状态代码以确保没有发生错误。您可以使用内置方法raise_for_status来执行此操作:https://requests.readthedocs.io/en/master/api/#requests.Response.raise_for_status
def downloadFile(downloadURL, directory, fileName, user=user, password=password):
if user is not None and password is not None:
request = requests.get(downloadURL, stream=True, auth=(user, password))
else:
request = requests.get(downloadURL, stream=True)
request.raise_for_status()
with open(directory + "\" + fileName, 'wb') as handle:
for block in request.iter_content(1024):
if not block:
break
handle.write(block)
你确定不存在诸如proxy/fw/etc之类的网络问题吗?
编辑:根据你的上述评论,我不确定这是否回答了你的实际问题。修订答案:
您访问的网页包含指向zip文件的链接。你说,这个链接和页面本身是一样的。但如果你在浏览器中点击它,它会下载文件,而不是再次到达HTML页面。这很奇怪,但可以用各种方式来解释。请复制/粘贴整个HTML页面代码(包括zip文件的链接),这可能有助于我们理解这个问题。