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.
https://assets.ellosgroup.com/i/ellos/b?$eg$&$em$&$ep$&$ed$&n=ell_1679093-02_Fs&mw=566&fmt=webp
http://searchengineland.com/wp-content/seloads/2014/07/google-logo-black-1920.jpg
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")