如何在下载 zipfile 时绕过警报窗口? [英] How to bypass the alert window while downloading a zipfile?

查看:42
本文介绍了如何在下载 zipfile 时绕过警报窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打开链接:https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip

此链接显示了窗口,我需要按确定"按钮并下载文件.

This link shows the window and I need to press the OK button and it downloads the file.

警报不是来自浏览器,而是来自页面本身.

The alert is not from the browser, it is from the page itself.

但是当我尝试脚本时:

from io import BytesIO
from zipfile import ZipFile
import requests


def get_zip(file_url):
    url = requests.get(file_url)
    zipfile = ZipFile(BytesIO(url.content))
    zipfile.extractall("")

file_link ='https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip'

get_zip(file_link)

这会引发错误:

zipfile.BadZipFile: File is not a zip file

当我尝试时:

import requests

url = r'https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip'
output = r'downloadedfile.zip'

r = requests.get(url)
with open(output, 'wb') as f:
    f.write(r.content)

这会下载显示确定"按钮的页面内容.知道如何解决这个问题:链接下载 zip 文件.

This downloads the content of the page showing the OK button. Any idea how to solve this:, the link downloads the zip file.

推荐答案

我相信您正在使用 selenium 接受答案,以下是您可以使用 selenium 执行的操作:

I believe you are accepting answer using selenium, Here's what you can do using selenium :

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

profile = webdriver.FirefoxProfile()

profile.set_preference("browser.download.folderList",1)
# 0 for desktop
# 1 for default download folder
# 2 for specific folder 
# You can specify directory by using profile.set_preference("browser.download.dir","<>")

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
profile.set_preference("browser.helperApps.alwaysAsk.force", False);

# If you don't have some download manager then you can remove these
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);

driver=webdriver.Firefox(firefox_profile=profile,executable_path="<>")

driver.get("https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip")
driver.find_element_by_id("butAgree").click()

这里我们设置了一些配置文件以禁用弹出、下载对话框.

Here we are setting some profiles to disable pop out, download dialog.

它在最新版本的 Firefox 和 3.141.0 版本的 selenium 中运行良好

It is working perfectly fine in latest version of Firefox and 3.141.0 version of selenium

这篇关于如何在下载 zipfile 时绕过警报窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆