图片的 urlretrieve returns HTTP 错误 403:禁止访问

urlretrieve for image returns HTTP Error 403: Forbidden

嘿,伙计们,我正在尝试使用 BeautifulSoup 获取图像,但这样做时出现错误:

这是我的代码:

imgUrl = "https://www.residentadvisor.net/images/events/flyer/2017/7/no-0713-986042-front.jpg"
try:
    urlretrieve(imgUrl, "testPhytonImg.jpg")
except FileNotFoundError as err:
    print("something wrong with local path")
    print(err)   # something wrong with local path
except HTTPError as err:
    print("something wrong with url")
    print(err)  # something wrong with url

这是我得到的错误: HTTP Error 403: Forbidden

为什么我得到这个?对图像的访问是否因为我的行为而被阻止,或者是否有其他方法?

这对我有用。您需要添加一个请求 header

import urllib.request
url_address = "https://www.residentadvisor.net/images/events/flyer/2017/7/no-0713-986042-front.jpg"
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
   'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
   'Accept-Encoding': 'none',
   'Accept-Language': 'en-US,en;q=0.8',
   'Connection': 'keep-alive'}
request_=urllib.request.Request(url_address,None,headers) #The assembled request
response = urllib.request.urlopen(request_)# store the response
#create a new file and write the image
f = open('00000001.jpg','wb')
f.write(response.read())
f.close()