【python基礎學習】---解析多層json,解析xml

一、以豆瓣的API接口爲例子,解析返回的json數據html

https://api.douban.com/v2/book/1220502

{
  "rating":{
    "max":10,
    "numRaters":16,
    "average":"7.3",
    "min":0
  },
  "subtitle":"",
  "author":[
    "Ranjan Bose",
    "武傳坤"
  ],
  "pubdate":"2005-1",
  "tags":[
    {
      "count":29,
      "name":"信息論",
      "title":"信息論"
    },
    {
      "count":22,
      "name":"密碼學",
      "title":"密碼學"
    },
    {
      "count":10,
      "name":"信息論與編碼",
      "title":"信息論與編碼"
    },
    {
      "count":8,
      "name":"計算機",
      "title":"計算機"
    },
    {
      "count":5,
      "name":"信息學",
      "title":"信息學"
    },
    {
      "count":1,
      "name":"通訊",
      "title":"通訊"
    },
    {
      "count":1,
      "name":"系統信息論",
      "title":"系統信息論"
    },
    {
      "count":1,
      "name":"欣",
      "title":"欣"
    }
  ],
  "origin_title":"",
  "image":"https://img3.doubanio.com/view/subject/m/public/s1166731.jpg",
  "binding":"平裝(無盤)",
  "translator":[
    "武傳坤"
  ],
  "catalog":"",
  "pages":"206",
  "images":{
    "small":"https://img3.doubanio.com/view/subject/s/public/s1166731.jpg",
    "large":"https://img3.doubanio.com/lpic/s1166731.jpg",
    "medium":"https://img3.doubanio.com/view/subject/m/public/s1166731.jpg"
  },
  "alt":"https://book.douban.com/subject/1220502/",
  "id":"1220502",
  "publisher":"機械工業出版社",
  "isbn10":"7111155343",
  "isbn13":"9787111155348",
  "title":"信息論、編碼與密碼學",
  "url":"https://api.douban.com/v2/book/1220502",
  "alt_title":"",
  "author_intro":"",
  "summary":"本書集中介紹了信息論、信源編碼、信道編碼和密碼等方面的知識,不只內容豐富,並且技術深度適當。適合做爲高等學校信息安全、電子工程及相關專業信息論和編碼課程的教材,從事相關工做的專業技術人員,也能從中受益。
  本書利用簡短的篇幅對信息論、編碼與密碼學等信息安全方面的知識,及其所涉及的數學理論進行了精闢論述,內容豐富,避免了太數學化而形成的晦澀難懂:經過翔實的例證由淺入深地闡明理論。
   本書特色
   對網格編碼調製(TCM)進行了詳細介紹,以加性白高斯操聲(AWGN)和衰退信道爲重點。
   經過實例闡述了信源編碼和信道編碼方面的知識。
   涵蓋了密碼學基本理論、密鑰和公鑰密碼學、現代加密標準和最新的研究趨勢。",
  "series":{
    "id":"1163",
    "title":"計算機科學叢書"
  },
  "price":"29.00元"
}
 

python2的解析代碼:python

import urllib2
import json
html = urllib2.urlopen(r'https://api.douban.com/v2/book/1220562')
hjson = json.loads(html.read())
print hjson['id']
print hjson['rating']['max']
print hjson['tags'][0]['name']

python3的解析代碼:json

import urllib.request
import json

if __name__ == '__main__':
    html = urllib.request.urlopen(r'https://api.douban.com/v2/book/1220502')
    hjson = json.load(html)
    print(hjson)
    # print(json.dump(hjson).replace('\'','\"'))
    print(hjson['series']['title'])

 2、python的字典(dict)轉化爲字符串(str):api

s = '{"host":"192.168.11.22", "port":3306, "user":"abc",\
                  "passwd":"123", "db":"mydb", "connect_timeout":10}'
print(type(s))
d = json.loads(s)
print(type(d))
______________________________
控制檯輸入:
<class 'str'>
<class 'dict'>

 3、讀取文件json,並解析安全

zhou.txt內容以下:bash

{"data":[{"id":"1134","name":"中石油加油站 (建港郵政所西南)","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區鸚鵡大道火車道南側","brandname":"中石油","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.25006,30.516361","lon":"114.25649912071","lat":"30.522662167959","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":10203},{"id":"8895","name":"中石化琴臺加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區琴臺路49號,琴臺大道與江城大道交叉","brandname":"中石化","type":"直營店","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.250105,30.555689","lon":"114.25651172095","lat":"30.561981100464","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":10258},{"id":"52166","name":"江鑽鄧甲村自然氣加氣站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區馬鸚路與江騰苑路交叉處,中青假日酒店附近","brandname":"不詳","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.2450171803,30.531668884","lon":"114.257943","lat":"30.544322","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":null,"fwlsmc":"","distance":9862},{"id":"53486","name":"道達爾加油站","area":"430033","areaname":"湖北省 武漢市 礄口區","address":"湖北省武漢市礄口區礄口路與仁壽路交叉處,東輝花園對面","brandname":"不詳","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.2517841979,30.5741290141","lon":"114.25819031949","lat":"30.580404505826","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":10760},{"id":"52165","name":"中石油馬鸚路加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區馬鸚路與江騰苑路交叉處,中青假日酒店附近","brandname":"中石油","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.2453546712,30.5315844542","lon":"114.258255","lat":"30.544189","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9831},{"id":"8270","name":"中石化金家墩加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市江漢區銀墩街,武漢郵政局附近","brandname":"中石化","type":"直營店","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.251981,30.612361","lon":"114.25841829676","lat":"30.618641616727","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":13064},{"id":"51463","name":"中石油金家墩加油站","area":"430040","areaname":"湖北省 武漢市 江漢區","address":"湖北省武漢市江漢區銀墩路與銀墩街交叉處,湖北郵政全夜航速遞局附近 ","brandname":"中石油","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.251983975,30.6154153127","lon":"114.25842360375","lat":"30.621697203353","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":13291},{"id":"49927","name":"道達爾常青路加油站","area":"430040","areaname":"湖北省 武漢市 江漢區","address":"湖北省武漢市江漢區常青路35號,常青路與雲彩路交叉口以西方向","brandname":"道達爾","type":"直營店","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.253907,30.606678","lon":"114.26034508395","lat":"30.612935608283","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"銀聯卡,信用卡支付,加油卡,便利店","distance":12511},{"id":"8892","name":"中石化五里加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區漢陽大道316號","brandname":"中石化","type":"直營店","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.254505,30.550811","lon":"114.26092817171","lat":"30.557056799261","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9732},{"id":"47702","name":"中石化玉帶門加油站","area":"430033","areaname":"湖北省 武漢市 礄口區","address":"湖北省武漢市礄口區京漢大道建國路特1號(祟仁路至橋口方向,地鐵集團附近)","brandname":"中石化","type":"直營店","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.254516,30.571964","lon":"114.26093030604","lat":"30.578207291699","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡,便利店","distance":10421},{"id":"3248","name":"中石化青年路加油站","area":"430033","areaname":"湖北省 武漢市 礄口區","address":"湖北省武漢市橋口區解放大道585號,解放大道與航空路交叉","brandname":"中石化","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.256956,30.589896","lon":"114.26338833234","lat":"30.596109715243","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":11161},{"id":"3250","name":"中石化青年路加油站","area":"430033","areaname":"湖北省 武漢市 礄口區","address":"湖北省武漢市礄口區建設大道與航空路路口東200米路北","brandname":"中石化","type":"其餘","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.250589627,30.5836655797","lon":"114.26354618034","lat":"30.596405592512","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"E93#":"5.7"},"fwlsmc":"加油卡","distance":11166},{"id":"52161","name":"兩湖路加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區兩湖路與新五里後堤交叉處,芳草苑附近","brandname":"不詳","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.2582592582,30.5212424922","lon":"114.26472225948","lat":"30.527444991614","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":9330},{"id":"54720","name":"中石化白沙洲北油氣合建站","area":"430061","areaname":"湖北省 武漢市 武昌區","address":"湖北省武漢市武昌區三環線白沙洲服務區內北側","brandname":"中石化","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.2650400597,30.4729104496","lon":"114.27150951472","lat":"30.478997158986","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":11000},{"id":"8893","name":"中石化白沙洲南站","area":"430061","areaname":"湖北省 武漢市 武昌區","address":"湖北省武漢市武昌區三環線下橋處,湖場村附近","brandname":"中石化","type":"其餘","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.26513,30.471974","lon":"114.27159905729","lat":"30.478058363651","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"93#":"5.7","0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡,便利店","distance":11060},{"id":"10495","name":"中石化岔馬路加油站","area":"430040","areaname":"湖北省 武漢市 江漢區","address":"湖北省武漢市江漢區解放大道1632號,解放大道與青年路交叉","brandname":"中石化","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.266801,30.580653","lon":"114.27326931334","lat":"30.586705763923","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":9801},{"id":"1137","name":"中石化交通加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區鸚鵡大道與新民一街交叉口,路東","brandname":"中石化","type":"其餘","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.260807775,30.5297201838","lon":"114.273748","lat":"30.541908","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"E93#":"5.7"},"fwlsmc":"加油卡","distance":8340},{"id":"47699","name":"中石油鸚鵡加油站","area":"430050","areaname":"湖北省 武漢市 漢陽區","address":"湖北省武漢市漢陽區鸚鵡大道與龜山北路一號,鸚鵡音像製品廠內","brandname":"中石油","type":"直營店","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.267544,30.555849","lon":"114.27401798863","lat":"30.561889149583","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"銀聯卡,信用卡支付,加?塗?,便利店,髮卡充值網點,銀聯卡充值,加油卡充值業務","distance":8629},{"id":"303","name":"中石化石橋加油站","area":"430040","areaname":"湖北省 武漢市 江漢區","address":"湖北省武漢市江漢區路姑嫂樹路和興業路交叉口向南,路東","brandname":"中石化","type":"其餘","discount":"打折加油站","exhaust":"國Ⅳ","position":"114.267576,30.624646","lon":"114.27408293469","lat":"30.630694367322","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"加油卡","distance":12979},{"id":"16573","name":"北湖西路三豐加油站 ","area":"430040","areaname":"湖北省 武漢市 江漢區","address":"湖北省武漢市江漢區北湖西路薄利通訊對面","brandname":"不詳","type":"其餘","discount":"非打折加油站","exhaust":"國Ⅳ","position":"114.268116,30.603663","lon":"114.27461064049","lat":"30.609697747818","price":{"E90":"5.1","E93":"5.4","E97":"5.8","E0":"5.05"},"gastprice":{"0#車柴":"5.35","E93#":"5.7"},"fwlsmc":"","distance":11245}],"pageinfo":{"pnums":20,"current":"1","allpage":8}}

 判斷是否爲字典或者列表:編碼

def fordict(contents):
    for key in contents.keys():
        # print(key)
        print(contents[key])
        #判斷是否爲列表
        if key == 'data' and isinstance(contents[key],list):
            for dd in range(len(contents[key])):
                # print(contents[key][dd])
                #判斷是否爲字典
                if contents[key][dd]['id'] == '1134' and  isinstance(contents[key][dd],dict):
                    print(fordict(contents[key][dd]))
                else:
                    return

使用遞歸:加密

if __name__ == '__main__':
    f = open("zhou.txt","r",encoding='utf-8')
    contents = json.load(f,encoding='utf-8')
    #是否爲字段
    print(isinstance(contents,dict))
    fordict(contents)
相關文章
相關標籤/搜索