Home > Net >  Reading and saving images from http://assets URLs
Reading and saving images from http://assets URLs

Time:04-13

I'm trying to download a bunch of images from different URLs and save them locally on my PC. Using the first two links below, I get the error: HTTPError: Forbidden and I have no idea why. Can it be because the site has protection from this?

Here is the script I used:

import urllib.request
imgURL = "one of the links below"

urllib.request.urlretrieve(imgURL, "C:/Users/temp/pic1.jpg")

URLs below. The first two gives the error, while the last one does not.

  1. https://assets.ellosgroup.com/i/ellos/b?$eg$&$em$&$ep$&$ed$&n=ell_1679093-02_Fs&mw=566&fmt=webp

  2. http://searchengineland.com/wp-content/seloads/2014/07/google-logo-black-1920.jpg

  3. https://media.istockphoto.com/photos/niagara-falls-picture-id508345252?k=6&m=508345252&s=170667a&w=0&h=bfGaBsL8VfBTLqJxFPwm8cXGaZLsgL34Ga4JSrhTtN0=

CodePudding user response:

Like you said it must a protection from bots from the website, the solution would be specifying a known user agent

import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
imgURL = "one of your links"
urllib.request.urlretrieve(imgURL, "C:/Users/temp/pic1.jpg")
  • Related