Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent downloads - Python

the plan is this:

I download a webpage, collect a list of images parsed in the DOM and then download these. After this I would iterate through the images in order to evaluate which image is best suited to represent the webpage.

Problem is that images are downloaded 1 by 1 and this can take quite some time.


It would be great if someone could point me in some direction regarding the topic.

Help would be very much appreciated.

like image 320
RadiantHex Avatar asked Mar 02 '10 01:03

RadiantHex


People also ask

How do I download multiple files concurrently in Python?

Download multiple files in parallel with Python To start, create a function ( download_parallel ) to handle the parallel download. The function ( download_parallel ) will take one argument, an iterable containing URLs and associated filenames (the inputs variable we created earlier).

How do I download multiple URLs at once?

As indicated, you can draw a rectangle around the links you want to select. This will highlight the links in yellow. From there you can either hit Enter to open the selected links in the same window, “Shift + Enter” to open in a new window, or “Alt + Enter” to download them.


1 Answers

Speeding up crawling is basically Eventlet's main use case. It's deeply fast -- we have an application that has to hit 2,000,000 urls in a few minutes. It makes use of the fastest event interface on your system (epoll, generally), and uses greenthreads (which are built on top of coroutines and are very inexpensive) to make it easy to write.

Here's an example from the docs:

urls = ["http://www.google.com/intl/en_ALL/images/logo.gif",
     "https://wiki.secondlife.com/w/images/secondlife.jpg",
     "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif"]

import eventlet
from eventlet.green import urllib2  

def fetch(url):
  body = urllib2.urlopen(url).read()
  return url, body

pool = eventlet.GreenPool()
for url, body in pool.imap(fetch, urls):
  print "got body from", url, "of length", len(body)

This is a pretty good starting point for developing a more fully-featured crawler. Feel free to pop in to #eventlet on Freenode to ask for help.

[update: I added a more-complex recursive web crawler example to the docs. I swear it was in the works before this question was asked, but the question did finally inspire me to finish it. :)]

like image 129
rdw Avatar answered Oct 19 '22 14:10

rdw