0

I've been trying to do some API queries to get some missing data in my DF. I'm using grequest library to send multiple request and create a list for the response object. Then I use a for loop to load the response in a json to retrieve the missing data. What I noticed is that when loading the data using .json() from the list directly using notition list[0].json() it works fine, but when trying to read the list and then load the response into a json, This error comes up : JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here's my code :

import requests 
import json
import grequests

ls = []
for i in null_data['name']:
    url = 'https://pokeapi.co/api/v2/pokemon/' + i.lower()
    ls.append(url)
rs  = (grequests.get(u) for u in ls)
s = grequests.map(rs)
#This line works
print(s[0].json()['weight']/10)

for x in s:
    #This one fails
    js = x.json()
    peso = js['weight']/10
    null_data.loc[null_data['name'] == i.capitalize(), 'weight_kg'] = peso
<ipython-input-21-9f404bc56f66> in <module>
     13 
     14 for x in s:
---> 15     js = x.json()
     16     peso = js['weight']/10
     17     null_data.loc[null_data['name'] == i.capitalize(), 'weight_kg'] = peso

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1 Answers1

0

One (or more) elements are empty.

So:

...
for x in s:
    if x != ""
        js = x.json()
        peso = js['weight']/10
        null_data.loc[null_data['name'] == i.capitalize(), 'weight_kg'] = peso
...

or

...
for x in s:
    try:
        js = x.json()
        peso = js['weight']/10
        null_data.loc[null_data['name'] == i.capitalize(), 'weight_kg'] = peso
    except json.JSONDecodeError as ex: print("Failed to decode(%s)"%ex)
...

The first checks if x is an empty string while the other tries to decode every one but upon an exception just prints an error message instead of quitting.

Minek Po1
  • 142
  • 1
  • 9