I am trying to search an API and when using the requests function I get an error that seems to indicate that there is nothing in JSON on the URL.When putting in my browser it works, and the function works in a similar piece of code.
This is my first ever time trying to code anything, so I getting to here was an effort but now I am just stuck and not sure where my error is. right now I am printing the URL, and when I put into my browser I find JSON code and the code works in another similar program I was making for testing.
import requests
import time
api = 'https://api.mojang.com/users/profiles/minecraft/'
f = open('Pokemon.txt', 'r')
for line in f:
url = (api + line)
print(url)
json_data = requests.get(url).json()
Result = (json_data)
print(result)
Here I get this back:
https://api.mojang.com/users/profiles/minecraft/Bulbasaur
Traceback (most recent call last):
File "C:\Users\Fierce-PC\Desktop\MC Name project\Pokemon.py", line 12, in <module>
json_data = requests.get(url).json()
File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Fierce-PC\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>>
You can clearly see that https://api.mojang.com/users/profiles/minecraft/Bulbasaur works if you click on it and it is in JSON format and I just don't really understand the problem.
What confused me more is that this code works
import urllib.parse
import requests
api = 'https://api.mojang.com/users/profiles/minecraft/'
Name = 'Bulbasaur'
url = (api + Name)
print(url)
json_data = requests.get(url).json()
print(json_data)
And it outputs this like I would want but it will not work with in the loop of looking through every Pokemon
{'id': '06e299358e2f44f1ad8c5f859d63973b', 'name': 'Bulbasaur'}
Sorry if this is a badly constructed post or that I am missing something really obvious.
EDIT: I edited both versions of the code to look like this:
json_data = requests.get(url)
print(json_data)
And for the print on the second line on the version that works I get this back
<Response [200]>
Where as on my main not working program I get this:
<Response [204]>
This as far as I can tell indicates that my code is not working though I am not too sure of the fix still though
Try this code. It iterates across the lines in your file, constructs the URL and then tries to get the data; on failure it should print the reason and keep going on the next line.
with open('Pokemon.txt') as fh:
for line in fh:
url = (api + line.strip())
print(url)
conn = requests.get(url)
if not conn.ok:
print("Failure on {}: {}".format(url, conn.reason))
continue
result = conn.json()
print(result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.