Advertisement
DKunth

Lesson_2_17_download_movie_poster_images

Mar 30th, 2022
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. df_list = []
  2. image_errors = {}
  3. headers = {
  4.     'User-Agent': 'My User Agent 1.0'
  5. }
  6. for title in title_list:
  7.     try:
  8.         # This cell is slow so print ranking to gauge time remaining
  9.         ranking = title_list.index(title) + 1
  10.         print(ranking)
  11.         page = wptools.page(title, silent=True)
  12.         # Your code here (three lines)
  13.         images = page.get().data['image']
  14.         # First image is usually the poster
  15.         print(first_image_url)
  16.         r = requests.get(first_image_url, headers = headers)
  17.         # Download movie poster image
  18.         i = Image.open(BytesIO(r.content))
  19.         image_file_format = first_image_url.split('.')[-1]
  20.         i.save(folder_name + "/" + str(ranking) + "_" + title + '.' + image_file_format)
  21.         # Append to list of dictionaries
  22.         df_list.append({'ranking': int(ranking),
  23.                         'title': title,
  24.                         'poster_url': first_image_url})
  25.    
  26.     # Not best practice to catch all exceptions but fine for this short script
  27.     except Exception as e:
  28.         print(str(ranking) + "_" + title + ": " + str(e))
  29.         image_errors[str(ranking) + "_" + title] = images
  30.  
  31. #I have updated the code of using a header when downloading the image.
  32. #I was able to download most of the images and only those images were not downloaded which were not proper image.
  33. #Please try this and let us know if this helps.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement