Skip to content

Instantly share code, notes, and snippets.

@xtream1101
Last active October 3, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtream1101/090aab1e00e245284a15af3f7cfaab05 to your computer and use it in GitHub Desktop.
Save xtream1101/090aab1e00e245284a15af3f7cfaab05 to your computer and use it in GitHub Desktop.
Quick example on how to download videos off getty images using only requests
import logging
import requests
import urllib
import urllib.parse
from parsel import Selector
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def download(url, save_path):
try:
with urllib.request.urlopen(urllib.request.Request(url)) as response,\
open(save_path, 'wb') as out_file:
data = response.read()
out_file.write(data)
except urllib.error.HTTPError as e:
save_path = None
# We do not need to show the user 404 errors
if e.code != 404:
logger.exception("Download Http Error {url}".format(url=url))
except Exception:
save_path = None
logger.exception("Download Error: {url}".format(url=url))
return save_path
def getty_video_search(term, max_page=1):
safe_term = urllib.parse.quote_plus(term)
for page_number in range(1, max_page + 1):
url = f'https://www.gettyimages.com/videos/{safe_term}?page={int(page_number)}'
r = requests.get(url)
source = Selector(text=r.text)
for link in source.css('a.asset-link'):
# Create download link
link_parts = link.xpath('@href').extract_first().split('/')
dl_link_name = link_parts[-2].replace('stock-footage', '')
dl_link = f'https://media.gettyimages.com/videos/{dl_link_name}-video-id{link_parts[-1]}'
filename = f'{link_parts[-2]}-{link_parts[-1]}.mp4'
print("Downloading", filename)
download(dl_link, filename)
getty_video_search('cookies')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment