这是Python 2上从API请求数据的代码
results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/ano64pm6?apikey=9ummN7C6KMHu9aErm49ixoy2ZySmaKCm"))
print results
打印
{u'count': 27,
u'frequency': u'Daily',
u'lastrunstatus': u'success',
u'lastsuccess': u'Fri Feb 27 2015 21:33:02 GMT+0000 (UTC)',
u'name': u'mmoga',
u'newdata': True,
u'nextrun': u'Sat Feb 28 2015 21:33:03 GMT+0000 (UTC)',
u'results': {u'collection1': [{u'price': u'\xa3\xa03.47'},
{u'price': u'\xa3\xa05.20'},
{u'price': u'\xa3\xa06.92'},
{u'price': u'\xa3\xa08.63'},
{u'price': u'\xa3\xa010.34'},
{u'price': u'\xa3\xa012.04'},
{u'price': u'\xa3\xa013.73'},
{u'price': u'\xa3\xa015.42'},
{u'price': u'\xa3\xa017.09'},
{u'price': u'\xa3\xa025.59'},
{u'price': u'\xa3\xa034.05'},
{u'price': u'\xa3\xa042.48'},
{u'price': u'\xa3\xa050.88'},
{u'price': u'\xa3\xa059.23'},
{u'price': u'\xa3\xa067.56'},
{u'price': u'\xa3\xa075.85'},
{u'price': u'\xa3\xa084.11'},
{u'price': u'\xa3\xa0100.73'},
{u'price': u'\xa3\xa0117.27'},
{u'price': u'\xa3\xa0133.75'},
{u'price': u'\xa3\xa0150.17'},
{u'price': u'\xa3\xa0166.51'},
{u'price': u'\xa3\xa0182.79'},
{u'price': u'\xa3\xa0198.99'},
{u'price': u'\xa3\xa0215.13'},
{u'price': u'\xa3\xa0231.20'},
{u'price': u'\xa3\xa0247.28'}]},
u'thisversionrun': u'Fri Feb 27 2015 21:32:48 GMT+0000 (UTC)',
u'thisversionstatus': u'success',
u'version': 3}
我需要打电话给价格,然后把它放在旁边。到目前为止,我尝试了一些非常简单的事情,
print requests[price]
以及
print requests[results/collection1/price]
我不知道如何做到这一点,我是编程的新手,我无法找到任何有用的教程,所以只是在正确的方向上一点是有帮助的
答案 0 :(得分:3)
import requests
results = requests.get("https://www.kimonolabs.com/api/ano64pm6?apikey=9ummN7C6KMHu9aErm49ixoy2ZySmaKCm").json()
print([x["price"] for x in results["results"]["collection1"]])
['£\xa03.47', '£\xa05.20', '£\xa06.92', '£\xa08.63', '£\xa010.34', '£\xa012.04', '£\xa013.73', '£\xa015.42', '£\xa017.09', '£\xa025.59', '£\xa034.05', '£\xa042.48', '£\xa050.88', '£\xa059.23', '£\xa067.56', '£\xa075.85', '£\xa084.11', '£\xa0100.73', '£\xa0117.27', '£\xa0133.75', '£\xa0150.17', '£\xa0166.51', '£\xa0182.79', '£\xa0198.99', '£\xa0215.13', '£\xa0231.20', '£\xa0247.28']
您可以使用requests调用.json()
直接获取json。
print(results["results"]["collection1"])
[{'price': '£\xa03.47'}, {'price': '£\xa05.20'}, {'price': '£\xa06.92'}, {'price': '£\xa08.63'}, {'price': '£\xa010.34'}, {'price': '£\xa012.04'}, {'price': '£\xa013.73'}, {'price': '£\xa015.42'}, {'price': '£\xa017.09'}, {'price': '£\xa025.59'}, {'price': '£\xa034.05'}, {'price': '£\xa042.48'}, {'price': '£\xa050.88'}, {'price': '£\xa059.23'}, {'price': '£\xa067.56'}, {'price': '£\xa075.85'}, {'price': '£\xa084.11'}, {'price': '£\xa0100.73'}, {'price': '£\xa0117.27'}, {'price': '£\xa0133.75'}, {'price': '£\xa0150.17'}, {'price': '£\xa0166.51'}, {'price': '£\xa0182.79'}, {'price': '£\xa0198.99'}, {'price': '£\xa0215.13'}, {'price': '£\xa0231.20'}, {'price': '£\xa0247.28'}]
results["results"]["collection1"]
返回一个dicts列表,然后在迭代列表时使用键price
从每个dict获取价格。
答案 1 :(得分:1)
使用for loop
prices = requests['results']['collection1']
for dict in prices:
price = dict['price']
#here you can use your price
print dict['price'] # prints price
使用compression list.
prices = [d['price'] for d in requests['results']['collection1']]