How to know if urllib.urlretrieve succeeds?

后端 未结 8 1922
长情又很酷
长情又很酷 2020-11-30 01:20

urllib.urlretrieve returns silently even if the file doesn\'t exist on the remote http server, it just saves a html page to the named file. For example:

8条回答
  •  眼角桃花
    2020-11-30 01:47

    According to the documentation is is undocumented

    to get access to the message it looks like you do something like:

    a, b=urllib.urlretrieve('http://google.com/abc.jpg', r'c:\abc.jpg')
    

    b is the message instance

    Since I have learned that Python it is always useful to use Python's ability to be introspective when I type

    dir(b) 
    

    I see lots of methods or functions to play with

    And then I started doing things with b

    for example

    b.items()
    

    Lists lots of interesting things, I suspect that playing around with these things will allow you to get the attribute you want to manipulate.

    Sorry this is such a beginner's answer but I am trying to master how to use the introspection abilities to improve my learning and your questions just popped up.

    Well I tried something interesting related to this-I was wondering if I could automatically get the output from each of the things that showed up in the directory that did not need parameters so I wrote:

    needparam=[]
    for each in dir(b):
        x='b.'+each+'()'
        try:
            eval(x)
            print x
        except:
            needparam.append(x)
    

提交回复
热议问题