下载
简介
每当页面下载附件时,会触发page.on("download")事件。所有这些附件都会被下载到一个临时文件夹中。您可以通过事件中的Download对象获取下载URL、文件名和有效负载流。
您可以使用browser_type.launch()中的downloads_path选项来指定下载文件的存储位置。
note
下载的文件会在产生它们的浏览器上下文关闭时被删除。
以下是处理文件下载的最简单方法:
- Sync
- 异步
# Start waiting for the download
with page.expect_download() as download_info:
# Perform the action that initiates download
page.get_by_text("Download file").click()
download = download_info.value
# Wait for the download process to complete and save the downloaded file somewhere
download.save_as("/path/to/save/at/" + download.suggested_filename)
# Start waiting for the download
async with page.expect_download() as download_info:
# Perform the action that initiates download
await page.get_by_text("Download file").click()
download = await download_info.value
# Wait for the download process to complete and save the downloaded file somewhere
await download.save_as("/path/to/save/at/" + download.suggested_filename)
变体
如果您不知道是什么触发了下载,您仍然可以处理该事件:
- Sync
- 异步
page.on("download", lambda download: print(download.path()))
async def handle_download(download):
print(await download.path())
page.on("download", handle_download)
请注意,处理该事件会分叉控制流,使脚本更难以跟踪。由于您的主控制流未等待此操作完成,您的场景可能会在下载文件时结束。
note
关于上传文件,请参阅uploading files部分。