嗨,我不是專家,這個問題讓我卡了這么長時間我希望這里的人可以幫助我我想從以下 json 檔案中提取值“interestExpense”:
{'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}
在這種情況下,結果應該是 -130000000 作為字串,但我試圖找到一種方法來創建一個包含所有這些浮點數的串列(或陣列),以便我可以決定選擇哪個,我不知道如何操作這個資料型別(json)例如
print(list[0])
應該回傳 17780000000(與 incomeBeforeTax 相關的值)這實際上可能嗎?輸出是從此代碼生成的:
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])
uj5u.com熱心網友回復:
如果您使用的是 python,則需要包含 json 模塊并將其決議為物件:
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"])
問候
L.
uj5u.com熱心網友回復:
將其復制并粘貼到 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'])
uj5u.com熱心網友回復:
好的,所以您發布的輸出片段來自這一行:
print(annual_is_stms[0])
如果你現在想要:-1803000000你應該這樣做:
print(annual_is_stms[0]['interestExpense'])
如果你想要:-130000000你應該這樣做:
print(annual_is_stms[0]['incomeTaxExpense'])
如果你想要:17780000000你應該這樣做:
print(annual_is_stms[0]['incomeBeforeTax'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465988.html
上一篇:final關鍵字簡介說明
