How to get data from a specific API website using Python
Question:
So I’m trying to randomly generate an insult from this API. I’m getting a response 200 from the API but I can’t seem to extract data
I’m using this code:
def get_insult():
res = requests.get('https://insult.mattbas.org/api/insult.txt')
print(res)
data_json = json.loads(res.json())
print(data_json)
get_insult()
Answers:
This should work. You don’t need to use json.loads()
as the response is not json
.
def get_insult():
res = requests.get('https://insult.mattbas.org/api/insult.txt')
data_json = res.text
print(data_json)
get_insult()
So I’m trying to randomly generate an insult from this API. I’m getting a response 200 from the API but I can’t seem to extract data
I’m using this code:
def get_insult():
res = requests.get('https://insult.mattbas.org/api/insult.txt')
print(res)
data_json = json.loads(res.json())
print(data_json)
get_insult()
This should work. You don’t need to use json.loads()
as the response is not json
.
def get_insult():
res = requests.get('https://insult.mattbas.org/api/insult.txt')
data_json = res.text
print(data_json)
get_insult()