如何在下载 zip 文件时绕过警报 window?
How to bypass the alert window while downloading a zipfile?
如果我打开link:https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip
此 link 显示 window,我需要按确定按钮,它会下载文件。
警报不是来自浏览器,而是来自页面本身。
但是当我尝试脚本时:
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)
这将下载显示“确定”按钮的页面内容。
知道如何解决这个问题:link 下载 zip 文件。
我相信您正在使用 selenium
接受答案,这是您可以使用 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()
我们在这里设置一些配置文件以禁用弹出、下载对话框。
它在最新版本的 Firefox 和 3.141.0 版本的 selenium 中运行良好
如果我打开link:https://dibbs2.bsm.dla.mil/Downloads/RFQ/Archive/ca210731.zip
此 link 显示 window,我需要按确定按钮,它会下载文件。
警报不是来自浏览器,而是来自页面本身。
但是当我尝试脚本时:
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)
这将下载显示“确定”按钮的页面内容。 知道如何解决这个问题:link 下载 zip 文件。
我相信您正在使用 selenium
接受答案,这是您可以使用 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()
我们在这里设置一些配置文件以禁用弹出、下载对话框。
它在最新版本的 Firefox 和 3.141.0 版本的 selenium 中运行良好