Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:27:27+00:00 2026-06-15T13:27:27+00:00

Recently I am working on a tiny crawler for downloading images on a url.

  • 0

Recently I am working on a tiny crawler for downloading images on a url.

I use openurl() in urllib2 with f.open()/f.write():

Here is the code snippet:

# the list for the images' urls
imglist = re.findall(regImg,pageHtml)

# iterate to download images
for index in xrange(1,len(imglist)+1):
    img = urllib2.urlopen(imglist[index-1])
    f = open(r'E:\OK\%s.jpg' % str(index), 'wb')
    print('To Read...')

    # potential timeout, may block for a long time
    # so I wonder whether there is any mechanism to enable retry when time exceeds a certain threshold
    f.write(img.read())
    f.close()
    print('Image %d is ready !' % index)

In the code above, the img.read() will potentially block for a long time, I hope to do some retry/re-open the image url operation under this issue.

I also concern on the efficient perspective of the code above, if the number of the images to be downloaded is somewhat big, using a thread pool to download them seems to be better.

Any suggestions? Thanks in advance.

p.s. I found the read() method on img object may cause blocking, so adding a timeout parameter to the urlopen() alone seems useless. But I found file object has no timeout version of read(). Any suggestions on this ? Thanks very much .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:27:28+00:00Added an answer on June 15, 2026 at 1:27 pm

    The urllib2.urlopen has a timeout parameter which is used for all blocking operations (connection buildup etc.)

    This snippet is taken from one of my projects. I use a thread pool to download multiple files at once. It uses urllib.urlretrieve but the logic is the same. The url_and_path_list is a list of (url, path) tuples, the num_concurrent is the number of threads to be spawned, and the skip_existing skips downloading of files if they already exist in the filesystem.

    def download_urls(url_and_path_list, num_concurrent, skip_existing):
        # prepare the queue
        queue = Queue.Queue()
        for url_and_path in url_and_path_list:
            queue.put(url_and_path)
    
        # start the requested number of download threads to download the files
        threads = []
        for _ in range(num_concurrent):
            t = DownloadThread(queue, skip_existing)
            t.daemon = True
            t.start()
    
        queue.join()
    
    class DownloadThread(threading.Thread):
        def __init__(self, queue, skip_existing):
            super(DownloadThread, self).__init__()
            self.queue = queue
            self.skip_existing = skip_existing
    
        def run(self):
            while True:
                #grabs url from queue
                url, path = self.queue.get()
    
                if self.skip_existing and exists(path):
                    # skip if requested
                    self.queue.task_done()
                    continue
    
                try:
                    urllib.urlretrieve(url, path)
                except IOError:
                    print "Error downloading url '%s'." % url
    
                #signals to queue job is done
                self.queue.task_done()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently was working with a subversion project that checked out code not only
I've recently been working on some code to catch errors throughout our code base
I have been recently working on an easy to use Syntax Highlighting system for
I am recently working with a F90 code project. I am using gfortran (linux
I was recently working on a piece of C++ code for a side project
Have recently started working with Sitecore 6.2. Is there a way to preserve code
I am recently working on better indexing concepts. as part of that, I am
I'm recently working on a WPF application that uses the datagrid from the WPF
I was recently working on a project that involving connection to a POP3 Server.
I was recently working on a project where I needed to convert a regular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.