Mass bitcoin address script not working due to json error - python

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)

Related

How do I prevent my program from throwing JSON Decode errors?

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

Raise JSON decode Error Expecting value: line 1 column 1 (char 0)

I am creating a website using Django .
Below is my code:
views.py:
for i in range(0,len(userdata)):
json_hall = requests.get(
"https://www.onebookingsystem.com/API/Admin/booking_list_id.php?id=%s" % userdata[i]['bookid'])
r = json_hall.json()
hall_data = json.loads(json_hall.text)
id_data[i]['bookid'] = hall_data[0]['bookid']
When i run i am getting the error like this.
File "D:\KarthikWorkSpace\Projects\Python\obs_admin\Application\obs_app\views.py", line 1968, in bookings_owner
hall_data = json.loads(json_hall.text)
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Any help would be appreciated.
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This usually means that the string you're trying to decode is empty, eg:
>>> import json
>>> json.loads('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/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)
Please, make sure the request you are sending is correct.
You could print or inspect response.text to confirm that.
When using requests, the .json() method on a response object already gives you a Python object (decoded from the literal response)
You don't need to parse again the data with json.loads. By the way, here's a more Pythonic way to iterate over your data:
for (idx, user) in enumerate(userdata):
response = requests.get(
"https://www.onebookingsystem.com/API/Admin/booking_list_id.php?id=%s" % user['bookid'])
hall_data = response.json()
id_data[idx]['bookid'] = hall_data[0]['bookid']

Expecting value: line 1 column 2 (char 1)

I have scraped some html and want to create a json doc. Here is the code I currently have:
with open(path.join(path.abspath(path.curdir),'Results\\html.txt'), 'r') as file:
for line in file.readlines():
if not line.strip():
continue
if re.findall(r'\"aggregateRating.*\"telephone\"',line):
reviews = re.findall(r'\[.*\]', line)
json_data = json.loads(str(reviews))
The error I get is: json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
Any help is appreciate. I have been stuck on this for awhile..
Your code is trying to load the string representation of a list as a valid json string; which of course will not work.
It is the same as trying to do this:
>>> json.loads(str(['hello world']))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/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 2 (char 1)
If you are trying to write the result as json; you need to do the opposite of loads, which is dumps:
>>> json.dumps(str(['hello world']))
'"[\'hello world\']"'

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Json error

for i in os.listdir("./saves/"):
path = './saves/' + i
if path.endswith(".json"):
with open(path, "r"):
config = json.loads(path)
ctime = config["Creation Time"]
Okay, this is the small code that's causing errors. I'm getting a json decoder error Here is the callback
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/acrobat/PycharmProjects/Jan9coderun/TranslatorVCS/Quiz.py", line 318, in saveGame
config = json.loads(path)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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)
My saves folder is in the same working directory as my code and has one quiz.json, one notes.txt, and one __init__.py file.

JSONDecode Error

I make a requests on IMDb to fetch movie information into JSON. Here's my code:
import json
import requests
url = "http://www.imdb.com/title/tt3385516/"
re = requests.get(url).json()
It get me an error which I don't know what to do with it:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/requests/models.py", line 799, in json
return json.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
I tried using
re = requests.get(url)
data = json.loads(re.text)
But it get me another error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 8 column 1 (char 7)
Response is HTML, not JSON so it can't be JSON decoded:
>>> r = requests.get('http://www.imdb.com/title/tt3385516/')
>>> r.headers['content-type']
'text/html;charset=UTF-8'

Categories