Skip to content

Instantly share code, notes, and snippets.

@ronknight
Created May 20, 2024 22:08
Download bulk images, PDFs, mp3s, etc. from multiple URLs on a txt file, using Python 3
import requests
import urllib
def download_url(file_url):
print("downloading: ",file_url)
# find "/" then assume that all the rest of the charaters after that represents the filename
# if url is www.test.com/abc/xyz/filename.jpg, the file name will be filename.jpg
file_name_start_pos = file_url.rfind("/") + 1
file_name = file_url[file_name_start_pos:]
r = requests.get(file_url, stream=True)
if r.status_code == requests.codes.ok:
with open(file_name, 'wb') as f:
for data in r:
f.write(data)
with open('files.txt', 'r') as urls:
for url in urls.readlines():
# go through all the URLs on the txt file separated by new line.
# remove newline character from the URL
url = url.rstrip("\n")
download_url(url)
https://pinoyitsolution.com/wp-content/uploads/2019/06/Tulips.jpg
https://pinoyitsolution.com/wp-content/uploads/2019/02/SEO-optimization.jpg
https://pinoyitsolution.com/wp-content/uploads/2019/10/sample-pdf.pdf
https://pinoyitsolution.com/wp-content/uploads/2019/10/16-11025u.wav
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment