簡體   English   中英

獲取貓圖片時出現403 Forbidden錯誤

[英]403 Forbidden error when getting cat images

我剛剛完成了一個狗照片程序的編程,經過幾個問題后它工作正常。 我決定制作一個修改版本,使用不同的 api 來代替提供貓圖像。 花了一秒鍾才弄清楚,但在更改格式后,我收到此錯誤: urllib.error.HTTPError: HTTP Error 403: Forbidden

此錯誤是一致的,並且每次都會發生。

這是我的全部代碼,因為這些答案似乎都沒有幫助。

import tkinter, urllib.request, json
from PIL import Image, ImageTk

#Create the main window
main = tkinter.Tk()
main.geometry('550x600+800+300')
main.title('Dogs')

#This is the list of dog image urls
urllist = []

#This is the list of dog images
doglist = []

#This is a pointer to the image in the list, which is used as the image in the label
dognumber = 0

#Set W and H to the max width and height we want for the images
W, H = 500, 460

#Resize the image to the max width and height
def resize_image(img): 
    ratio = min(W/img.width, H/img.height)
    return img.resize((int(img.width*ratio), int(img.height*ratio)), Image.ANTIALIAS)

#Change dog number by -1 to go back a dog, then set the image to the new dog
def last_image():
    global doglist, dognumber

    #This is pretty self explanatory
    dognumber -= 1

    #If the dog number is less than 0, set it to 0 so there's no negative indexing in dog list
    if dognumber < 0:
        dognumber = 0

    #Set the image to the new dog
    l.config(image=doglist[dognumber])

def fetch_image():
    global doglist, dognumber, urllist
    try:
        #If the dog number is less than the length of the list, simply increment it by 1 and set the image to the new dog. This allows you to go back a dog, and then go forward again, instead of generating a new dog every time
        dognumber += 1
        l.config(image=doglist[dognumber])
    except IndexError:
        #Even if we are on the last dog on the list, that try script will add one to the dog number, so we need to subtract one to get the correct index
        dognumber -= 1

        #Get the actual image of the dog we want
        dogapi = urllib.request.Request(f'https://api.thecatapi.com/v1/images/search')
        dogapi.add_header("x-api-key", "blahblahblah12312")
        dogjson = urllib.request.urlopen(dogapi).read()
        dogdict = json.loads(dogjson)
        url = dogdict[0]['url']
        print(url)

        #Convert the image to a PIL image
        #error right here, stack overflow didnt help ):
        m = urllib.request.urlopen(url)
        mpi = resize_image(Image.open(m))
        tkimg = ImageTk.PhotoImage(mpi)

        #Add the new dog to the list, as well as the url 
        doglist.append(tkimg)
        urllist.append(url)

        #Increment the dog number, and set the image to the new dog
        dognumber += 1
        l.config(image=tkimg)
        l.image = tkimg

def save_dogs():
    global urllist

    #Open the file to write to
    w = open('/'.join(__file__.split("\\")[:-1]) + '/doglist.txt', 'w')

    #Write the urls to the file
    w.write('\n'.join(urllist))

#Load Label and Buttons
l = tkinter.Label(main, image=tkinter.PhotoImage(), width=W, height=H)
b = tkinter.Button(main, text='Next Dog', command=fetch_image)
b2 = tkinter.Button(main, text='Last Dog', command=last_image)
b3 = tkinter.Button(main, text='Save Dogs', command=save_dogs)

def preloaddogs():
    global doglist, dognumber, urllist
    for _ in range(5):
        #This does the same thing as fetch_image, but it doesn't increment the dog number so you dont end up on the last dog, and it sets the image shown to the first dog
        dogapi = urllib.request.urlopen(f'https://dog.ceo/api/breeds/image/random')
        dogjson = dogapi.read()
        dogdict = json.loads(dogjson)
        url = dogdict['message']
        m = urllib.request.urlopen(url)
        mpi = resize_image(Image.open(m))
        tkimg = ImageTk.PhotoImage(mpi)
        doglist.append(tkimg)
        urllist.append(url)
        url = urllist[0]
        m = urllib.request.urlopen(url)
        mpi = resize_image(Image.open(m))
        tkimg = ImageTk.PhotoImage(mpi)
        l.config(image=tkimg)
        l.image = tkimg

#Load the widgets, and then preload the first 5 dogs
l.pack()
b.place(x=350, y=500)
b2.place(x=150, y=500)
b3.pack()

#preloaddogs()

main.mainloop()

這是完整的錯誤:

Traceback (most recent call last):
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "g:\My Drive\Code\Projects\Dog\cats.pyw", line 58, in fetch_image
    m = urllib.request.urlopen(url)
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 525, in open
    response = meth(req, response)
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 634, in http_response
    response = self.parent.error(
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 563, in error
    return self._call_chain(*args)
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\doubl\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

我嘗試添加 api 密鑰,但仍然出現相同的錯誤。 然后嘗試將其添加到 header,同樣的故事。

由於這些答案都不起作用,我添加了我的整個代碼。 希望這會有所幫助。

“thecatapi”需要一個 API 密鑰,您似乎沒有提供密鑰,這就是您收到 403 錯誤的原因

嘗試添加 API 密鑰作為請求 Header
你可以在thecatapi 網站上得到一個

import requests
from PIL import Image
from io import BytesIO

token = "token"

def getRandomCat():
    url = "https://api.thecatapi.com/v1/images/search"
    response = requests.get(url, params={"x-api-key": token})
    return response.json()[0]["url"]

if __name__ == '__main__':
    # get url of image
    url = getRandomCat()
    # get content from the url with the image
    response = requests.get(url)
    # convert and open Image (conversion with io)
    image = Image.open(BytesIO(response.content))

嘗試更改使用的 API 端點。 如果這解決了問題,則可能是這是一張您無權訪問的高級照片,或者您只是多次調用此端點。

您可能缺少 API 密鑰。 在postman中測試API URL

https://thecatapi.com/v1/images/search?api_key=YOUR_API_KEY

在此處獲取您的 API 密鑰: Cat API 密鑰注冊

通常,我會先在 Postman 中進行測試,方法是在 Headers 中提供x-api-key並在 Param 中提供搜索查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM