简体   繁体   English

使用 Python 在 visual studio 代码中调用 API

[英]Calling an API in visual studio code using Python

I am trying to call the marvel API from here: https://developer.marvel.com我想从这里打电话给奇迹 API: https://developer.marvel.com

The code I wrote for it:我为它写的代码:

import requests, json
from pprint import pprint

# Call an API
url = "http(s)://gateway.marvel.com/"
response = requests.get(url)
response.raise_for_status() # check for errors

# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)

When I run it, I receive the error: requests.exceptions.HTTPError: 409 Client Error: Conflict for url:当我运行它时,我收到错误: requests.exceptions.HTTPError: 409 Client Error: Conflict for url:

It is from rest api来自 rest api

import requests
import json
from pprint import pprint

# Call an API
response = requests.get('https://jsonplaceholder.typicode.com/users')
response.raise_for_status()  # check for errors
print(response.status_code)
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)

I'm assuming the url you posted is just a placeholder?我假设您发布的 url 只是一个占位符? The correct format should be something like https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey .正确的格式应该类似于https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey

You also need the to specify an endpoint, in my example I used the /characters endpoint.您还需要指定端点,在我的示例中,我使用了 /characters 端点。

Try removing the response.raise_for_status().尝试删除 response.raise_for_status()。 It should continue and give you an error message with what is wrong (according to the docs, almost all errors will give you a status code of 409).它应该继续并给你一条错误消息,说明有什么问题(根据文档,几乎所有错误都会给你一个状态代码 409)。

import requests, json
from pprint import pprint

# Call an API
url = "https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey."
response = requests.get(url)
# response.raise_for_status() # check for errors

# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)

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

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