How to know if urllib.urlretrieve succeeds?

后端 未结 8 1924
长情又很酷
长情又很酷 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:42

    :) My first post on StackOverflow, have been a lurker for years. :)

    Sadly dir(urllib.urlretrieve) is deficient in useful information. So from this thread thus far I tried writing this:

    a,b = urllib.urlretrieve(imgURL, saveTo)
    print "A:", a
    print "B:", b
    

    which produced this:

    A: /home/myuser/targetfile.gif
    B: Accept-Ranges: bytes
    Access-Control-Allow-Origin: *
    Cache-Control: max-age=604800
    Content-Type: image/gif
    Date: Mon, 07 Mar 2016 23:37:34 GMT
    Etag: "4e1a5d9cc0857184df682518b9b0da33"
    Last-Modified: Sun, 06 Mar 2016 21:16:48 GMT
    Server: ECS (hnd/057A)
    Timing-Allow-Origin: *
    X-Cache: HIT
    Content-Length: 27027
    Connection: close
    

    I guess one can check:

    if b.Content-Length > 0:
    

    My next step is to test a scenario where the retrieve fails...

提交回复
热议问题