I'm new to python and APIs and I'm using this tutorial to learn with last-fm. I'm getting these errors:
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 89, in <module>
total_pages = int(response.json()['artists']['#attr']['totalPages'])
File "/Applications/anaconda3/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Here's the code in question:
#initializes reponses list
responses = []
page = 1
total_pages = 70
while page <= total_pages:
payload = {
'method': 'chart.gettopartists',
'limit': 500,
'page': page
}
#print
print("Requesting page {}/{}".format(page, total_pages))
#clear clear_output
clear_output(wait=True)
#make the API call
response = lastfm_get(payload)
#error check
if response.status_code != 200:
print(reponse.text)
break
#extract pagination info
page = int(response.json()['artists']['#attr']['page'])
total_pages = int(response.json()['artists']['#attr']['totalPages'])
# append
responses.append(response)
#if its not cached, sleep
if not getattr(response, 'from_cache', False):
time.sleep(0.25)
#increment
page += 1
This function: lastfm_get just does requests.get with the appropriate params and this url 'http://ws.audioscrobbler.com/2.0/'
From other posts I get the impression the program is getting non-JSON objects, but when I print
total_pages = int(response.json()['artists']['#attr']['totalPages']) I get the correct number.
edit:
this is the function:M
def lastfm_get(payload):
#define headers and url
headers = {'user-agent': USER_AGENT}
url = 'http://ws.audioscrobbler.com/2.0/'
#add API key and format
payload['api_key'] = API_KEY
payload['format'] = 'json'
response = requests.get(url, headers = headers, params = payload)
return response
edit2:
I added this code to check if I was getting a JSON object, and am getting a type error after about 200 pages:
#check object
try:
json_test = json.loads(lastfm_get(payload))
except TypeError:
print("not a json object")
Error:
Requesting page 256/7743
Requesting page 259/7743
Requesting page 275/7743
Requesting page 294/7743
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 94, in <module>
page = int(response.json()['artists']['#attr']['page'])
File "/Applications/anaconda3/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Applications/anaconda3/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I got a type error when I tried with except ValueError:
Traceback (most recent call last):
File "/Users/vikram03/Desktop/Python_learning/fm.py", line 84, in <module>
json_test = json.loads(lastfm_get(payload))
File "/Applications/anaconda3/lib/python3.8/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not Response
EDIT3: I get this error when testing the output of lastfm_get:
TypeError: Object of type Response is not JSON serializable
Unsure how to resolve this.
I'm not sure why the page numbers are skipping either
Related
I have tested around 1000 bitcoin addresses and there were no problems, however after trying to test around 40,000 addresses it gives me this error
from requests import get
import pandas as pd
import json
def make_api_url():
data = pd.read_csv('bitcoinaddr.csv')
Wallet_Address = (data.loc[:,"Address"])
BASE_URL = "https://blockchain.info/balance"
for addresses in Wallet_Address:
addresses = '|'.join(Wallet_Address)
url = BASE_URL + f"?active={addresses}" #error
return url
get_balance_url = make_api_url()
response = get(get_balance_url)
j_data = response.json()
finalbalance=[]
tx=[]
totalreceived=[]
new_dataset = pd.DataFrame.from_dict(j_data, orient='index')
print(new_dataset)
finalbalance=new_dataset['final_balance'].to_list()
tx=new_dataset['n_tx'].to_list()
totalreceived=new_dataset['total_received'].to_list()
data['Final Balance']=finalbalance
data['n_tx']=tx
data['Total Received']=totalreceived
new_dataset.to_csv('CheckedBTCAddress.csv',index=True,header=True)
# https://blockchain.info/balance?active=$address
The error code shows
Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py",
line 971, in json
return complexjson.loads(self.text, **kwargs) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json_init_.py",
line 346, in loads
return _default_decoder.decode(s) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\decoder.py",
line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\Forensic\AppData\Local\Programs\Python\Python310\lib\json\decoder.py",
line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:\Users
Scripting\BTCAddressBulkChecker.py", line 21, in
data = response.json() File "C:\UsersPython\Python310\lib\site-packages\requests\models.py",
line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1
(char 0)
here is the full error message for when i run the code that is under this error message:
PS C:\Users\Admin> & C:/Users/Admin/AppData/Local/Programs/Python/Python39/python.exe c:/Users/Admin/Desktop/dadjoke.py
Traceback (most recent call last):
File "c:\Users\Admin\Desktop\dadjoke.py", line 9, in <module>
data = json.loads(str(question.text ))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
PS C:\Users\Admin>
here is my code:
import requests
import json
params = {"q":"joke"}
url = "https://icanhazdadjoke.com"
question = requests.get(url, params)
if question.status_code == 200:
data = json.loads(str(question.text ))
print(data)
I've just take a look at the API docs of the website.
Since you didn't specify your Accept header, the website will return a HTML response as default
Solution:
headers = {
"Accept": "application/json"
}
url = "https://icanhazdadjoke.com"
r = requests.get(url, headers = headers)
I am using requests module to run a curl command, not sure why its prefixing ')]}\' in the front of the output, it makes r.json() fail as shown below.
When I paste the URL in a browser and execute, it downloads a .json file, and that file has same characters in the front, Am I missing some option? I need to process the output as json.
>>> r = requests.get('https://gerrit-review-server/a/changes/?q=status:open%20project:myproj/test/a_proj%20change:1510&o=CURRENT_REVISION', verify=False, auth=HTTPBasicAuth('user','pass'),
>>>
>>> r.text
')]}\'\n[{"id":"myproj%2Ftest%2Fa_proj~master~I15790ba05690e0a9984cb05bce06574645274966","project":"myproj/test/a_proj","branch":"master","hashtags":[],"change_id":"I15790ba05690e0a9984cb05bce06574645274966","subject":"Test","status":"NEW","created":"2021-01-27 19:38:57.000000000","updated":"2021-03-21 14:19:42.000000000","submit_type":"MERGE_IF_NECESSARY","mergeable":true,"insertions":1,"deletions":0,"total_comment_count":0,"unresolved_comment_count":0,"has_review_started":true,"_number":1510,"owner":{"_account_id":10008339},"current_revision":"fe3cc60cc66ad6f20c631ee818ccd91955c69d37",<..deleted..>,"description":"Rebase"}},"requirements":[]}]\n'
>>> r.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/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)
>>>
As Carcigenicate says, it seems that the json is wrongly formatted. You can try the loads, and if it fails try to correct it:
r = requests.get(some_url)
try:
data = r.json()
except json.JSONDecodeError:
# cut everything in front of the first "\n"
raw_data = r.text.split("\n", maxsplit=1)[1]
# cut everything behind the last "\n"
raw_data = raw_data.rsplit("\n", maxsplit=1)[0]
# try to load again the json
# If it fails it will raise the exception again
data = json.loads(raw_data)
I'm run my code to extract required data from RNA central database based RNAcentral accession number.
Sometimes in the middle, the program stops and start showing this error:
Error Obtained
File "C:\Users\soura\Desktop\Thesis\Saurav Data\Data_Extraction_RNA_Central.py", line 53, in <module>
data = page.json()['results']
File "D:\programs\anaconda3\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "D:\programs\anaconda3\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "D:\programs\anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\programs\anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
This is the part of my code where I'm having an error
import requests
import time
ids = ['URS0000D57BCE', 'URS0000D57BCE', 'URS0000EEF870', 'URS00009C33DE']
for id in range(len(ids)):
# Accessing the database cross-reference url with exception handling and extracting info
page = ''
while page == '':
try:
page = requests.get('http://rnacentral.org/api/v1/rna/{}/xrefs.json'.format(ids[id]))
break
except:
print('Connection refused by the server at id {} and position {}'.format(ids[id], ids.index(ids[id])))
print('Lets me sleep for 5 seconds')
time.sleep(5)
continue
# Extracting json content from above url
data = page.json()['results']
Later I modified my code like this but still, I'm getting the same error:
I Changed page = '' to page = None
I thought since the error is for the None value of the page. I wrote a while loop in such a way until there is the None value of page, the code will re-run again and again.
import requests
import time
ids = ['URS0000D57BCE', 'URS0000D57BCE', 'URS0000EEF870', 'URS00009C33DE']
for id in range(len(ids)):
# Accessing the database cross-reference url with exception handling and extracting info
page = None
while page == None:
try:
page = requests.get('http://rnacentral.org/api/v1/rna/{}/xrefs.json'.format(ids[id]))
break
except:
print('Connection refused by the server at id {} and position {}'.format(ids[id], ids.index(ids[id])))
print('Lets me sleep for 5 seconds')
time.sleep(5)
continue
# Extracting json content from above url
data = page.json()['results']
Now, I'm getting this error:
Traceback (most recent call last):
File "Data_Extraction_RNA_Central.py", line 61, in <module>
data = page.json()['results']
File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Please if anyone can help, it will be a great help for me. :-)
import requests
I am trying to send data to an API which works fine but all of a sudden i start getting JSON error
This is the code
def payment(phone, receiver_phone, amount):
req_header = os.environ.get('APP_KEY')
payload = {
'receiver_phone': receiver_phone,
'amount': amount,
'payer_phone': phone
}
res = requests.post('https://sspay.com/payment?key={0}'.format(req_header), data=payload)
return res.json()
print(payment('07XXXXXX', '0XXXXXXXXX', '1'))
This is the output i get
Traceback (most recent call last):
File "test.py", line 25, in <module>
print(payment('07xxxxx', '09xxxxxxxx', '1'))
File "test.py", line 14, in payment
return res.json()
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/home/pc/.virtualenvs/talk/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
use the code below - it will make you to find the issue
req_header = os.environ.get('APP_KEY')
res = requests.post('https://sspay.com/payment?key={0}'.format(req_header), data=payload)
if res.status_code == 200:
data = res.json()
return data
else:
print('We have a problem. status code : {}'.format(res.status_code))
you are not checking the API response (status code)
what if req_header is None