【发布时间】:2020-07-29 11:29:45
【问题描述】:
我正在尝试使用 python 从 json url 链接中提取数据元素。下面是代码。当我尝试提取元素时它正在部分工作
response = urllib.request.urlopen(url) data = json.loads(response.read()) print("planId",data[0]["planId"]) #Gives result as planId PWR93173MBE1 print("postcode",data[0]["postcode"]) # Gives result as postcode 2000 print("tariffType", data[0]["tariffType"]) This gives me error.
另外,如果我想提取费用中的PlanType等其他元素,我该怎么做呢?
[
{
"planData":{
"planType":"M",
"tariffType":"SR",
"contract":[
{
"pricingModel":"SR",
"benefitPeriod":"Ongoing",
"coolingOffDays":10,
"additionalFeeInformation":"This offer provides access to wholesale prices, utilises your Powerbank to smooth wholesale market volatility and Powerwatch to warn of higher prices. For more information on this and any other standard fees, visit our website www.powerclub.com.au",
"fee":[
{
"description":"Annual Membership payable each year for each of your business premises taking supply.",
"amount":79,
"feeType":"MBSF",
"percent":0,
"feeTerm":"A"
},
{
"description":"Cost for providing a paper bill",
"amount":2.5,
"feeType":"PBF",
"percent":0,
"feeTerm":"F"
},
{
"description":"Disconnection fee",
"amount":59.08,
"feeType":"DiscoF",
"percent":0,
"feeTerm":"F"
},
{
"description":"Reconnection Fee",
"amount":59.08,
"feeType":"RecoF",
"percent":0,
"feeTerm":"F"
},
{
"description":"Meter Read - Requested by Customer",
"amount":12.55,
"feeType":"OF",
"percent":0,
"feeTerm":"F"
}
],
"planId":"PWR93173MBE1",
"planType":"E#B#PWR93173MBE1",
"postcode":2000
}
]
【问题讨论】:
-
您能否详细说明您遇到的问题、期望的结果以及您需要从费用中提取什么?
-
从费用中我想要描述和金额。我试图获取描述 print("Fees", data[0]['planData']['fee']['description']) 但它不起作用。希望我能够解释这个问题。
标签: python json web-crawler