How to avoid these errors while fetching api in python - python

import requests
import json
response = requests.get('http://dataservice.accuweather.com/currentconditions/v1/2807435?apikey=secret')
print(response)
x = response.json()
y = json.dumps(x)
z = json.loads(y)
a = z['WeatherText']
print(a)
yt = input("Press Enter to close")
This code is for retrieving weather data from accuweather. When I run the file, i get an error:
TypeError: list indices must be integers or slices, not str
A sample image of the initial json file is:
This image is the output
Please help me with the error

You only need x, but you also need to recognise that it is a list:
a = x[0]['WeatherText']

Variable z is a list of dict. Therefore you need to go to the first position by:
a = z[0]['WeatherText']

Related

Exctract a value from a Json file(python)

Hi i'm not an expert and this problem kept me stuck for such a long time I hope that someone here can help me
i would like to exctract the value "interestExpense" from the following json file:
{'incomeBeforeTax': 17780000000,
'minorityInterest': 103000000,
'netIncome': 17937000000,
'sellingGeneralAdministrative': 5918000000,
'grossProfit': 16507000000,
'ebit': 10589000000,
'endDate': 1640908800,
'operatingIncome': 10589000000,
'interestExpense': -1803000000,
'incomeTaxExpense': -130000000,
'totalRevenue': 136341000000,
'totalOperatingExpenses': 125752000000,
'costOfRevenue': 119834000000,
'totalOtherIncomeExpenseNet': 7191000000,
'netIncomeFromContinuingOps': 17910000000,
'netIncomeApplicableToCommonShares': 17937000000}
In this case the result should be -130000000 as a string but i m trying to find a way to create an list(or an array) with all those floats so that i can decide which one to pick, i have no idea how to manipulate this kind of data(json)
For example
print(list[0])
should return 17780000000(the value associated with incomeBeforeTax)
is this actually possible?
The output is generated from this code:
annual_is_stms=[]
url_financials ='https://finance.yahoo.com/quote/{}/financials?p{}'
stock= 'F'
response = requests.get(url_financials.format(stock,stock),headers=headers)
soup = BeautifulSoup(response.text,'html.parser')
pattern = re.compile(r'\s--\sData\s--\s')
script_data = soup.find('script',text=pattern).contents[0]
script_data[:500]
script_data[-500:]
start = script_data.find("context")-2
json_data =json.loads(script_data[start:-12])
json_data['context']['dispatcher']['stores']['QuoteSummaryStore'].keys()
#all data relative financials
annual_is=json_data['context']['dispatcher']['stores']['QuoteSummaryStore']['incomeStatementHistory']['incomeStatementHistory']
for s in annual_is:
statement = {}
for key, val in s.items():
try:
statement[key] = val['raw']
except TypeError:
continue
except KeyError:
continue
annual_is_stms.append(statement)
print(annual_is_stms[0])
If you are using python, you need to include the json module and parse it as an object:
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
Regards
L.
Ok, so the output snippet you posted comes from this line:
print(annual_is_stms[0])
If you now want the: -1803000000 you should do:
print(annual_is_stms[0]['interestExpense'])
If you want the: -130000000 you should do:
print(annual_is_stms[0]['incomeTaxExpense'])
and if you want the: 17780000000 you should do:
print(annual_is_stms[0]['incomeBeforeTax'])
Copy and paste this into Python.
data = {'incomeBeforeTax': 17780000000,
'minorityInterest': 103000000,
'netIncome': 17937000000,
'sellingGeneralAdministrative': 5918000000,
'grossProfit': 16507000000,
'ebit': 10589000000,
'endDate': 1640908800,
'operatingIncome': 10589000000,
'interestExpense': -1803000000,
'incomeTaxExpense': -130000000,
'totalRevenue': 136341000000,
'totalOperatingExpenses': 125752000000,
'costOfRevenue': 119834000000,
'totalOtherIncomeExpenseNet': 7191000000,
'netIncomeFromContinuingOps': 17910000000,
'netIncomeApplicableToCommonShares': 17937000000}
print(data['interestExpense'])

How to create an empty numpy array with semi-specified dims?

I am trying to read data from a server like this:
with requests.Session() as s:
data = {}
r = s.get('https://something.com' , json = data ).json()
training_set1 = np.empty([-1,4])
training_set1[:,0] = r["o"]
training_set1[:,1] = r["h"]
training_set1[:,2] = r["l"]
training_set1[:,3] = r["c"]
But I don't know the length of arrays, so I used -1 then got this error message:
ValueError: negative dimensions are not allowed
How can I fix this code? The response r is a JSON object:
{"t":[1322352000,1322438400],
"o":[123,123],
"h":[123,123],
"l":[123,123],
"c":[123,123]}
that I am trying to rearrange it to a numpy array.
Numpy arrays have fixed sizes. You cannot initialize a dynamic sized array. What you can do is use a list of lists and later convert the list to a numpy array.
Something like this should work assuming r["x"] is a list. (Untested code)
with requests.Session() as s:
data = {}
r = s.get('https://something.com' , json = data ).json()
t_set1 = []
t_set1.append(r["o"])
t_set1.append(r["h"])
t_set1.append(r["l"])
t_set1.append(r["c"])
training_set1 = np.array(t_set1)
Edit: Edited for the order "o","h","l",""c after OP edited the question
You cannot declare a numpy array with an unknown dimension. But you can declare it in one single operation:
training_set1 = np.array([r["o"], r["o"], r["h"], r["l"]])
or even better:
training_set1 = np.array([r[i] for i in "oohl"])

Json from url - array in array

I want take data from API from polish http by json format. But, I have problem take data from array in array.
From "normal" json I can took data, but this json have struture as 'krs_podmioty.id' => 'blabla' <= I have problem with . (dot) and array in array.
I try get data from https://api-v3.mojepanstwo.pl/dane/krs_podmioty/10186.json?layers[]=dzialalnosci&layers[]=reprezentacja.
You can decode on: http://freeonlinetools24.com/json-decode (and past text from http).
It's public website and data.
If you will look it, I want data from segment:
'krs_podmioty'.person_id' => array ( 0 => '14439' .... 11 => '1233301' )
import urllib.request
import json
res = urllib.request.urlopen('https://api-v3.mojepanstwo.pl/dane/krs_podmioty/10186.json?layers[]=dzialalnosci&layers[]=reprezentacja')
res_body = res.read()
j = json.loads(res_body.decode("utf-8"))
for item in j['data']:
ucmdbId = (item['krs_podmioty'])
print('Id podmioty: '.format(ucmdbId))
exit(0)
In perfect situation I need print list of all "krs_podmioty.person_id"
Thank you very much!
import requests
import json
result = requests.get('https://api-v3.mojepanstwo.pl/dane/krs_podmioty/10186.json?layers[]=dzialalnosci&layers[]=reprezentacja').json()
ids = result['data']['krs_podmioty.person_id']
for id in ids:
print('Id podmioty: ' + id)
Try this:
for item in j['data']['krs_podmioty.person_id']:
ucmdbId = item
print('Id podmioty: {0} '.format(ucmdbId))
j['data'] contained all of the objects in the 'data' array in which you could call for the krs_podmioty.person_id key to get its corresponding value array.

String Manipulation for Json webscraping

I am trying to scrape a website and have all the data needed in very long matrices which were obtained through requests and json imports.
I am having issues getting any output.
Is it because of the merge of two strings in requests.get()?
Here is the part with the problem, all things used were declared at the start of the code.
balance=[]
for q in range(len(DepositMatrix)):
address= requests.get('https://ethplorer.io/service/service.php?data=' + str(DepositMatrix[q][0]))
data4 = address.json()
TokenBalances = data4['balances'] #returns a dictionary
balance.append(TokenBalances)
print(balance)
Example of DepositMatrix - list of lists with 4 elements, [[string , float, int, int]]
[['0x2b5634c42055806a59e9107ed44d43c426e58258', 488040277.1535826, 660, 7103],
['0x05ee546c1a62f90d7acbffd6d846c9c54c7cf94c', 376515313.83254075, 2069, 12705]]
I think the error is in this part:
requests.get('https://ethplorer.io/service/service.php?data=' + str(DepositMatrix[q][0]))
This change doesnt help either:
requests.get('https://ethplorer.io/service/service.php?data=' + DepositMatrix[q][0])
Like I said in my comment, I tried your code and it worked for me. But I wanted to highlight some things that could help your code be clearer:
import requests
import pprint
DepositMatrix = [['0x2b5634c42055806a59e9107ed44d43c426e58258', 488040277.1535826, 660, 7103],
['0x05ee546c1a62f90d7acbffd6d846c9c54c7cf94c', 376515313.83254075, 2069, 12705]]
balance=[]
for deposit in DepositMatrix:
address = requests.get('https://ethplorer.io/service/service.php?data=' + deposit[0])
data4 = address.json()
TokenBalances = data4['balances'] #returns a dictionary
balance.append(TokenBalances)
pprint.pprint(balance)
For your loop, instead of creating a range of the length of your list (q) and then using this q to get the information back from your list, it's simpler to get each element directly (for deposit in DepositMatrix:)
I've used the pprint module to ease the visualization of your data.

Converting to string not producing required value

I have some code that is using the 'gdshortener' module to produce a shortened version of my source URLS:
import simplejson
import httplib2
import twitter
import gdshortener
from random import randint
print("Python will now attempt to submit tweets to twitter...")
try:
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='')
b = 0
for a in range(0, 1): #only range 0-1 for this question, actually 1-21
b = b + 1
a = randint(1,60000000)
randint
print ("a = ", a)
aa = str(a)
s1 = gdshortener.ISGDShortener()
print s1.shorten(url = 'http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html', custom_url = aa)
ss1 = str(s1)
status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + ss1 + " #propellerhead #synapse")
print("Tweets submitted successfully!")
except Exception,e:
print str(e)
print("Twitter submissions have failed!!!")
I am using a random number generator to produce six digit numbers which are then fed to the custom_url parameter of this module. That works fine and I get a series of pseudo-random numbers. However when I try and concatenate my tweet string, my dynamic short URL and some hashtags I get an error that I cannot concatenate string and integer values.
So I have then created the variable 'ss1' which is the string of 's1', however this now produces a tweet like this:
The new Synapse Antidote Rack Extension:<gdshortener.gdshortener.ISGDShortener object at 0x000000000542AA20> #propellerhead #synapse
How can i get it so that the tweet produced is:
The new Synapse Antidote Rack Extension: http://is.gd/58077181 #propellerhead #synapse
Thanks
Checked the module and discovered that it returns a tuple. See the following to extract the proper URL.
Code:
import gdshortener
s1 = gdshortener.ISGDShortener()
x1 = s1.shorten(url='http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html')[0]
print x1
Result:
http://is.gd/KKxmFd
[Finished in 0.8s]
Notice how I added a [0] at the end of the shorten. This is because shorten returns a tuple, which we can index similar to a list.
Hope this helps.

Categories