Save File from URL in Python – Aspose.HTML

Saving a file from a URL is a practical skill that allows you to download and store files for various purposes, such as offline access, sharing, and archiving.

This article will walk you through the process of how to save file from URL by leveraging the Aspose.HTML Python library’s network capabilities. The example provided demonstrates how to send a network request to retrieve the file and save it to your local file system.

How to Save File from URL

To save file from URL, you need to know its URL and have a network operations handler that is appropriate for its protocol. Aspose.HTML for Python via .NET library provides you with convenient functionality for processing URLs. To use it, you need to create an empty HTML document and call the network request handler, as shown in the following Python example:

  1. Create an empty HTML document using the default HTMLDocument() constructor. Although the document is not directly used for the network operation, it is necessary to access the network capabilities provided by the document.context.network object.
  2. Create an instance of the Url class with the path to the resource you want to save.
  3. Use the RequestMessage(uri) constructor to create a new instance of the RequestMessage class. This instance represents the network request to be sent. The uri parameter is passed to the constructor, specifying the URL to which the request will be sent.
  4. Use the context.network.send(message) method to send the network request and retrieve the response. The response is checked to ensure it was successful.
  5. If the response is successful, the code constructs the local file path. The file is then written to the specified path in binary mode using response.content.read_as_byte_array() to read the content.

Let’s look at how to save file from URL using the Aspose.HTML Python library:

 1import os
 2from aspose.html.net import *
 3from aspose.html import*
 4
 5# Define output directory
 6output_dir = "output/"
 7os.makedirs(output_dir, exist_ok=True)
 8
 9# Create a blank document
10document = HTMLDocument()
11
12# Create a URL with the path to the resource you want to save
13url = Url("https://docs.aspose.com/html/images/handlers/message-handlers.png")
14
15# Create a file request message
16request = RequestMessage(url)
17
18# Extract file from URL
19response = document.context.network.send(request)
20
21# Check whether the response is successful
22if response.is_success:
23    # Save the file to a local file system
24    file_path = os.path.join(output_dir, os.path.basename(url.pathname))
25    with open(file_path, 'wb') as file:
26        file.write(response.content.read_as_byte_array())

Why Save Files from URLs

Downloading files from URLs offers various benefits and serves several purposes. Here are some of the main reasons why you might want to save files:

Download our Aspose.HTML for Python via .NET library to successfully, quickly, and easily manipulate your HTML documents. The Python library can create, modify, extract data, convert, and render HTML documents without the need for external software. It supports popular file formats such as EPUB, MHTML, XML, SVG, and Markdown and can render to PDF, DOCX, XPS, and Image file formats.

Aspose.HTML offers HTML Web Applications, which are an online collection of free converters, mergers, SEO tools, HTML code generators, URL tools, web accessibility checkers, and more. The applications work on any operating system with a web browser and do not require any additional software installation. Easily convert, merge, encode, generate HTML code, extract data from the web, or analyze web pages for SEO, wherever you are. Use our collection of HTML Web Applications to perform everyday tasks and make your workflow flawless!

Text “HTML Web Applications”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.