程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python request URL JSON and multi nesting

編輯:Python
1、 call url Interface
#!/usr/bin/python
import requests
import json
def get_news():
""" Get Kingsoft ICIBA Daily Sentence , English and translation """
url = "http://open.iciba.com/dsapi/"
# r = requests.get(url)
r = requests.get(url).text
d = json.loads(r)
print(type(r))
print(type(d))
print(d)
print(d.keys())
# Take out key Corresponding value
print(d['note'])
if __name__ == "__main__":
res = get_news()

result

<class 'str'>
<class 'dict'>
{'sid': '4535', 'tts': 'https://staticedu-wps.cache.iciba.com/audio/04b09bf426c7b87095281d00a5f0941d.mp3', 'content': 'Those who follow the light will eventually be radiant.', 'note': ' Those who follow the light will be radiant .', 'love': '0', 'translation': ' New daily sentence ', 'picture': 'https://staticedu-wps.cache.iciba.com/image/e8c6e99671f9797106bc2a60f6cture2': 'https://staticedu-wps.cache.iciba.com/image/0dfd35fcad9190747d8bd3cd2c91c758.jpg', 'caption': ' ICIBA Daily Sentence ', 'dateline': '2022-06-26', 's_pv': '0', 'sp_pv':'fenxiang_img': 'https://staticedu-wps.cache.iciba.com/image/d83cc3372ad490a481170a92050767bb.jpg', 'picture3': 'https://staticedu-wps.cache.iciba.com/image/05041b6b2fdc4b862d1f15bdd8b947c2.jpg', 'picture4': 'https://staticedu-wps.cache.iciba.com/image/b1e20fd1986c4caf666d78f8d0cb865a.jpg', 'tags': []}
dict_keys(['sid', 'tts', 'content', 'note', 'love', 'translation', 'picture', 'picture2', 'caption', 'dateline', 's_pv', 'sp_pv', 'fenxiang_img', 'picture3', 'picture4', 'tags'])
Those who follow the light will be radiant .
 

2、 Take out the multi-level nesting

import json

s = '{"data":{"yesterday":{"date":"11 On Tuesday, ","high":" The high temperature 33℃","fx":" Southwest wind ","low":" low temperature 26℃","fl":" breeze ","type":" A shower "},"city":" Hangzhou ","aqi":"56","forecast":[{"date":"12 Wednesday, Sunday ","high":" The high temperature 36℃","fengli":" Breeze level ","low":" low temperature 28℃","fengxiang":" Southwest wind ","type":" cloudy "},{"date":"13 Thursday, Sunday ","high":" The high temperature 37℃","fengli":" Breeze level ","low":" low temperature 28℃","fengxiang":" Southwest wind ","type":" cloudy "},{"date":"14 Sunday, Friday ","high":" The high temperature 36℃","fengli":" Breeze level ","low":" low temperature 28℃","fengxiang":" The south wind ","type":" cloudy "},{"date":"15 Saturday, Sunday ","high":" The high temperature 36℃","fengli":" Breeze level ","low":" low temperature 27℃","fengxiang":" The south wind ","type":" cloudy "},{"date":"16 Sunday Sunday ","high":" The high temperature 36℃","fengli":" Breeze level ","low":" low temperature 27℃","fengxiang":" Southeast wind ","type":" Fine "}],"ganmao":" The weather conditions are suitable , The chance of catching a cold is low . But please avoid being in an air-conditioned room for a long time , In case of a cold .","wendu":"31"},"status":1000,"desc":"OK"}'

t = json.loads(s)

print(t['data']['yesterday']['high'])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved