Answer the question
In order to leave comments, you need to log in
How can the script be optimized?
I needed to download the whole album from VK. 120+ photos, too lazy. I installed a plugin for Chrome, got a list of links to photos, put them in a text file.
I think, let me write a PHP script that will do everything for me. But then I remember that I want to learn Python, so without thinking twice I decided on the tool. Remembering the recently read Dive into Python and googling a couple of questions regarding the jump itself, I wrote the following code.
# Imports
import urllib
import os
# Initialize downloader
web = urllib.URLopener()
# Path/files
cwd = os.getcwd()
urls = os.path.join(cwd, 'data.txt')
# Read the file
sock = open(urls)
data = [item.strip() for item in sock.readlines()]
sock.close()
# Download files
for url in data:
# Get the filename
basename = os.path.basename(url)
# Destination..
dest = os.path.join(cwd, 'temp', basename)
# Process download
web.retrieve(url, dest)
# Print we are done
print 'Done %s' % dest
Answer the question
In order to leave comments, you need to log in
> The question is: what could be done better? Maybe somewhere it could be easier?
Use wget.
-i, --input-file=FILE download URLs found in local or external FILE.
For training, you can do exception handling so that the script does not crash on the first broken link, but throws an error in stderr
If the goal is to practice programming, then you can rewrite it for parallel downloading.
In general, look towards asyncore or greenlets.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question