1import requests
2
3url = 'http://google.com/favicon.ico'
4r = requests.get(url, allow_redirects=True)
5open('google.ico', 'wb').write(r.content)
6
1import requests
2
3def is_downloadable(url):
4 """
5 Does the url contain a downloadable resource
6 """
7 h = requests.head(url, allow_redirects=True)
8 header = h.headers
9 content_type = header.get('content-type')
10 if 'text' in content_type.lower():
11 return False
12 if 'html' in content_type.lower():
13 return False
14 return True
15
16print(is_downloadable('https://www.youtube.com/watch?v=9bZkp7q19f0'))
17# >> False
18print(is_downloadable('http://google.com/favicon.ico'))
19# >> True
20