python request image code example

Example 1: get image image memeory size in url inpyton requests

import urllib.request, io
url = 'http://www.elsecarrailway.co.uk/images/Events/TeddyBear-3.jpg'
path = urllib.request.urlopen(url)
meta = path.info()

>>>meta.get(name="Content-Length")
'269898' # ie  269kb

Example 2: save image requests python

import requests

def is_downloadable(url):
    """
    Does the url contain a downloadable resource
    """
    h = requests.head(url, allow_redirects=True)
    header = h.headers
    content_type = header.get('content-type')
    if 'text' in content_type.lower():
        return False
    if 'html' in content_type.lower():
        return False
    return True

print(is_downloadable('https://www.youtube.com/watch?v=9bZkp7q19f0'))
# >> False
print(is_downloadable('http://google.com/favicon.ico'))
# >> True