Search code examples
pythonbeautifulsoupurllib

Beautiful Soup - urllib.error.HTTPError: HTTP Error 403: Forbidden


I am trying to download a GIF file with urrlib, but it is throwing this error:

urllib.error.HTTPError: HTTP Error 403: Forbidden

This does not happen when I download from other blog sites. This is my code:

import requests
import urllib.request

url_1 = 'https://goodlogo.com/images/logos/small/nike_classic_logo_2355.gif'

source_code = requests.get(url_1,headers = {'User-Agent': 'Mozilla/5.0'})    

path = 'C:/Users/roysu/Desktop/src_code/Python_projects/python/web_scrap/myPath/'

full_name = path + ".gif"    
urllib.request.urlretrieve(url_1,full_name)

Solution

  • Don't use urllib.request.urlretrieve. Instead, use the requests library like this:

    import requests
    
    url = 'https://goodlogo.com/images/logos/small/nike_classic_logo_2355.gif'
    
    path = "D:\\Test.gif"
    
    response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
    
    file = open(path, "wb")
    
    file.write(response.content)
    
    file.close()
    

    Output:

    enter image description here

    Hope that this helps!