How to know if urllib.urlretrieve succeeds?

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

    Consider using urllib2 if it possible in your case. It is more advanced and easy to use than urllib.

    You can detect any HTTP errors easily:

    >>> import urllib2
    >>> resp = urllib2.urlopen("http://google.com/abc.jpg")
    Traceback (most recent call last):
    <>
    urllib2.HTTPError: HTTP Error 404: Not Found
    

    resp is actually HTTPResponse object that you can do a lot of useful things with:

    >>> resp = urllib2.urlopen("http://google.com/")
    >>> resp.code
    200
    >>> resp.headers["content-type"]
    'text/html; charset=windows-1251'
    >>> resp.read()
    "<>"
    

提交回复
热议问题