Python and json, How to print data["key"] -
i have simple scraping code:
import urllib import re import json htmltext = urllib.urlopen("http://dx.com/p/getproductinforealtime?skus=48616") htmltext = json.load(htmltext) print htmltext
it outputs:
{u'data': [{u'sku': 48616, u'isshowdiscount': false, u'currencycode': u'usd', u'issoldout': false, u'adddate': u'10/28/2010', u'discount': 0, u'currencysymbol': u'us$', u'price': u'4.20', u'listprice': u''}], u'success': true}
i can not figure out how data in correct format can use terms on left side of colons key terms on right side.
i to
print htmltext["sku"] 48616
or
print htmltext["price"] 4.20
any ideas on this?
what have there dictionary key data
,
so access inner list with:
htmltext[u"data"]
then access "sku"
, need access dictionary within list in "data"
's value.
inner_dict = htmltext[u"data"][0] print(inner_dict[u"sku"])
you define function such as:
def get_data(dict_index, key): return htmltext[u"data"][dict_index][key] print(get_data(0, u"sku"))
Comments
Post a Comment