gpt4 book ai didi

python - 通过python上的selenium headless chrome下载文件

转载 作者:行者123 更新时间:2023-11-28 17:12:07 25 4
gpt4 key购买 nike

因此,使用 selenium 通过 headless chrome 下载文件的问题似乎仍然是一个问题 asked here一个多月前没有答案。但我不明白他们是如何实现错误线程中的 js 的。是否有我可以添加的选项或当前的修复程序?原始错误页面位于 here截止到今天 2017 年 10 月 22 日,我所有的东西都是最新的

在 python 中:

from selenium import webdriver


options = webdriver.ChromeOptions()

prefs = {"download.default_directory": "C:/Stuff",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}

options.add_experimental_option("prefs", prefs)
options.add_argument('headless')
driver = webdriver.Chrome(r'C:/Users/aaron/chromedriver.exe', chrome_options = options)

# test file to download which doesn't work
driver.get('http://ipv4.download.thinkbroadband.com/5MB.zip')

如果删除 headless 选项,这没有问题。

我尝试下载的实际文件是位于 .aspx URL 的 PDF。我通过执行 .click() 下载它们,它工作得很好,除了 headless 版本。 hrefs 是 javascript do_postback 脚本。

最佳答案

你为什么不找到 anchor href,然后使用 get 请求来下载文件。这样它将在 headless 模式下工作并且会更快。我已经在 C# 中完成了。

def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
#f.flush() commented by recommendation from J.F.Sebastian
return local_filename

关于python - 通过python上的selenium headless chrome下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46878471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com