Home > Article > Backend Development > How to Download Files from the Web Using Python 3?
When extracting URLs from JAD files for downloading JAR files, we encounter an error due to the URL being stored as a string type. To resolve this issue, we explore methods to download files with string URLs in Python 3.
Retrieving Web Page Contents:
To fetch the contents of a web page into a variable, we can use urllib.request.urlopen and read the response:
<code class="python">import urllib.request url = 'http://example.com/' response = urllib.request.urlopen(url) data = response.read() # bytes object text = data.decode('utf-8') # str object</code>
Downloading and Saving Files:
For straightforward file downloads and saving, urllib.request.urlretrieve is optimal:
<code class="python">import urllib.request # Download and save file from url to file_name urllib.request.urlretrieve(url, file_name)</code>
Alternatively, you can obtain the local path and response headers:
<code class="python">file_name, headers = urllib.request.urlretrieve(url)</code>
Optimal Solution Using urlopen and shutil.copyfileobj:
The recommended approach is to utilize urllib.request.urlopen to retrieve a file-like HTTP response object and copy it to a file using shutil.copyfileobj:
<code class="python">import urllib.request import shutil # Download and save file from url to file_name with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file: shutil.copyfileobj(response, out_file)</code>
Alternative Approach for Small Files:
For smaller files, it's possible to store the entire download in a bytes object and write it to a file:
<code class="python">import urllib.request # Download and save file from url to file_name with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file: data = response.read() # bytes object out_file.write(data)</code>
Extracting Compressed Data on the Fly:
You can also extract compressed data on the fly if the HTTP server supports random file access:
<code class="python">import urllib.request import gzip # Read first 64 bytes of .gz file at url url = 'http://example.com/something.gz' with urllib.request.urlopen(url) as response: with gzip.GzipFile(fileobj=response) as uncompressed: file_header = uncompressed.read(64) # bytes object</code>
The above is the detailed content of How to Download Files from the Web Using Python 3?. For more information, please follow other related articles on the PHP Chinese website!