RPA / Robocorp
Data: extract / Download files
Download a single file
tasks.py
import os
from robocorp.tasks import task
from robocorp import browser
from robocorp.browser import download
@task
def download_single_file():
"""
Download a single file from a website.
This example demonstrates how to:
- Configure download settings
- Download a file by clicking on a link
- Wait for the download to complete
- Optionally rename the file
"""
# Configuration variables
download_url = "https://github.com/robocorp/example-browser-automation/archive/refs/heads/master.zip"
download_folder = "output"
new_filename = "robocorp-example.zip" # Optional: leave empty to keep original filename
# Create download folder if it doesn't exist
os.makedirs(download_folder, exist_ok=True)
# Configure browser with download settings
page = browser.goto(
download_url,
# Tell the browser where to save downloaded files
downloadsPath=download_folder
)
# The simpler approach - direct URL download:
with download.download(page) as dl_info:
# Direct download from URL
page.goto(download_url)
# Wait for download to complete and get the downloaded file path
download_path = dl_info.wait_for_download()
print(f"Downloaded file: {download_path}")
# Rename the file if needed
if new_filename:
file_extension = os.path.splitext(download_path)[1]
new_path = os.path.join(download_folder, new_filename)
os.rename(download_path, new_path)
print(f"Renamed to: {new_path}")
Download multiple files
tasks.py
import os
from robocorp.tasks import task
from robocorp import browser
from robocorp.browser import download
@task
def download_multiple_files():
"""
Download multiple files from a list of URLs.
This example demonstrates how to:
- Create a loop to download multiple files
- Ensure the destination folder exists
- Handle each download with proper naming
"""
# Configuration variables
download_urls = [
"https://github.com/robocorp/example-browser-automation/archive/refs/heads/master.zip",
"https://github.com/robocorp/example-hello-world/archive/refs/heads/master.zip",
"https://github.com/robocorp/example-web-store-order-processor/archive/refs/heads/master.zip"
]
download_folder = "output/multiple_downloads"
# Create download folder if it doesn't exist
os.makedirs(download_folder, exist_ok=True)
# Configure browser with download settings
browser_context = browser.context(
# Tell the browser where to save downloaded files
acceptDownloads=True,
downloadsPath=download_folder
)
# Create a new page
page = browser_context.new_page()
# Download each file in the list
for index, url in enumerate(download_urls, 1):
print(f"Downloading file {index} of {len(download_urls)}: {url}")
# Set up download listener
with download.download(page) as dl_info:
# Navigate to URL to trigger download
page.goto(url)
# Wait for download to complete
download_path = dl_info.wait_for_download()
# Create a custom filename based on the index
filename = f"download_{index}{os.path.splitext(download_path)[1]}"
new_path = os.path.join(download_folder, filename)
# Rename the file
os.rename(download_path, new_path)
print(f"Downloaded and saved as: {new_path}")
print("All downloads completed successfully!")