简体   繁体   English

Python:response.status_code 在使用 google 运行时总是返回 400 api

[英]Python: response.status_code alway return 400 when run with google api

import requests
import json

# Define the API endpoint and necessary headers and parameters
url = "https://www.googleapis.com/customsearch/v1"
# url = "https://yande.re/post/similar"

api_key = "replace with your api"
headers = {"Content-Type": "image/jpeg"}
params = {"type": "similar", "cx": "d47119ff66e004d6b", "key":api_key}

# Read the image file and convert it to binary
with open("C:\\Users\\Administrator\\Downloads\\theme\\3f559715-f407-47de-8540-1cd9e4fdc56c.jpg", "rb") as f:
    image_data = f.read()

# Send the POST request
response = requests.post(url, headers=headers, params=params, data=image_data)

# Check the status code of the response
if response.status_code == 200:
    print('ok')
    # Parse the JSON response
    data = json.loads(response.text)
    # Extract the relevant data from the response
    similar_images = data["similar_images"]
else:
        print(response.status_code)
        print('no')

what wrong with my code?我的代码有什么问题? My url was wrong?我的url打错了? i alway get no when run it运行时我总是no

when I try with another URL like https://yande.re/post/similar i get return.当我尝试使用另一个 URL 时,例如https://yande.re/post/similar我得到回报。 what was happen?发生了什么事?

https://www.googleapis.com/customsearch/v1 is a GET Request API. But you are trying to do POST method on this API. https://www.googleapis.com/customsearch/v1是一个 GET 请求 API。但是您正在尝试对这个 API 执行 POST 方法。

Here is the sample of GET Request for this API这是此 API 的 GET 请求示例

url = "https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start={start}"

# make the API request
data = requests.get(url).json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM