1

I am learning to grab pictures from URL and found a Q&A here. Since it's Python 3, I changed import urllib to import urllib.request and

urllib.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

to

urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg").

It doesn't work! It says AttributeError: 'module' object has no attribute 'urlretrieve'

I then changed urllib.urlretrieve to def request.urlretrieve and got SyntaxError: invalid syntax

I also tried def urlretrieve and it's not working either.

Could anyone tell me how to get it right? Thanks!

What if the URL contains more than one pic?

6
  • 1
    In Python 3, urllib.request.urlretrieve should work. Can you post some of your code? Commented Aug 11, 2014 at 18:58
  • @hlt, it does not. you need from urllib import request Commented Aug 11, 2014 at 19:02
  • import urllib.request and then urllib.request.urlretrieve works perfectly fine on my 3.4.1 Commented Aug 11, 2014 at 19:04
  • @hlt, I missed the second part of the scrambled text, I thought the OP only had an import urllib but I imagine the urllib.request.urlretrieve was not combined with the correct import statement Commented Aug 11, 2014 at 19:08
  • 1
    @rain, you did not write it correctly this morning, python does not have off days ;) Commented Aug 11, 2014 at 19:11

2 Answers 2

3

You need to use:

from urllib import request
request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

You can use also use:

 import urllib.request
 urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")
Sign up to request clarification or add additional context in comments.

Comments

0

Since you have imported urllib.request module, doesn't it seem obvious that you should call its method urlretrieve(args) as urllib.request.urlretrieve(args).

  1. When you type import <module>, you call its method using <module>.method(args) (As specified in above code).

  2. Alternatively, you can import the module using from <module> import * and then call its method using method(args). Eg -

    from urllib.request import *

    urlretrieve(args).

  3. Another way is to only import the method you need to use in your program using from <module> import method and then call the method using method(args). Eg -

from urllib.request import urlretrieve and then call the method using

urlretrieve(args).

The urllib.request module for Python 3 is well documented here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.