'Bytes object has not attribute json with requests
I am extracting BTC
history data from a website and want to store these results. I want to run this in a function and then use map
because it's much faster than storing everything within a dict by using a loop. However, I get the following error:
1 def storeBitcoin(response):
2 bitcoin = defaultdict(list)
----> 3 for row in range(0, len(response.json()['data']['KdataInfo'])):
4 bitcoin['timestamp'].append(response.json()['data']['KdataInfo'][row]['T'])
5 bitcoin['open'].append(response.json()['data']['KdataInfo'][row]['O'])
AttributeError: 'bytes' object has no attribute 'json'
Here's what I have tried:
params = {
'token': 'jTf9y32fLnC5GG64AIQNFZzlCiS73L7L',
'codeid': '3223607',
'interval': '35',
'from': '1651751935',
'to': '1651838395',
}
response = requests.get('https://www.btcc.com/quot/history', params=params)
def storeBitcoin(response):
bitcoin = defaultdict(list)
for row in range(0, len(response.json()['data']['KdataInfo'])):
bitcoin['timestamp'].append(response.json()['data']['KdataInfo'][row]['T'])
bitcoin['open'].append(response.json()['data']['KdataInfo'][row]['O'])
bitcoin['closed'].append(response.json()['data']['KdataInfo'][row]['C'])
bitcoin['high'].append(response.json()['data']['KdataInfo'][row]['H'])
bitcoin['low'].append(response.json()['data']['KdataInfo'][row]['L'])
return bitcoin
list(map(storeBitcoin, response)) #error starts here
I have tested my function by using the loop inside and storing it in a dict with the results from requests and it does work. Why am I getting the error?
working code:
params = {
'token': 'jTf9y32fLnC5GG64AIQNFZzlCiS73L7L',
'codeid': '3223607',
'interval': '35',
'from': '1651751935',
'to': '1651838395',
}
response = requests.get('https://www.btcc.com/quot/history', params=params)
bitcoin = defaultdict(list)
for row in range(0, len(response.json()['data']['KdataInfo'])):
bitcoin['timestamp'].append(response.json()['data']['KdataInfo'][row]['T'])
bitcoin['open'].append(response.json()['data']['KdataInfo'][row]['O'])
bitcoin['closed'].append(response.json()['data']['KdataInfo'][row]['C'])
bitcoin['high'].append(response.json()['data']['KdataInfo'][row]['H'])
bitcoin['low'].append(response.json()['data']['KdataInfo'][row]['L'])
Managed to get it to work with the following thanks to Scott's comments:
i = 0
#bitcoin = defaultdict(list)
full_response = []
while i < 8:
i+=1
val_begin = np.array([1651751935])
val_end = np.array([1651838395])
np.add.at(val_begin, [0], i)
np.add.at(val_end, [0], i)
params = {
'token': 'jTf9y32fLnC5GG64AIQNFZzlCiS73L7L',
'codeid': '3223607',
'interval': '35',
'from': str(val_begin[0]),
'to': str(val_end[0]),
}
response = requests.get('https://www.btcc.com/quot/history', params=params, cookies=cookies, headers=headers)
full_response.append(response)
list(map(storeBitcoin, full_response))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|