2

嘿伙计们,我正在尝试使用 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

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

4

1 回答 1

7

这对我有用。您需要添加请求标头

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()
于 2017-07-11T13:44:50.650 回答