python爬虫requests json与字典对象互相转换
生活随笔
收集整理的這篇文章主要介紹了
python爬虫requests json与字典对象互相转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1 import requests 2 import json 3 ''' 4 json.loads(json_str) json字符串轉換成字典 5 json.dumps(dict) 字典轉換成json字符串 6 7 ''' 8 # 這是一個ajax發起的get請求,獲取一個json對象 9 r = requests.get("https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?os=ios&for_mobile=1&start=0&count=18&loc_id=108288&_=0") 10 json_response = r.content.decode() # 獲取r的文本 就是一個json字符串 11 12 # 將json字符串轉換成dic字典對象 13 dict_json = json.loads(json_response) 14 print(type(dict_json)) 15 16 # 將字典轉換成json字符串 17 str_json = json.dumps( dict_json ) 18 print(type(str_json)) 19 20 # 字典轉換成json 存入本地文件 21 with open('./a.txt','w') as f: 22 # 設置不轉換成ascii json字符串首縮進 23 f.write( json.dumps( dict_json,ensure_ascii=False,indent=2 ) )?
轉載于:https://www.cnblogs.com/Lin-Yi/p/7640147.html
總結
以上是生活随笔為你收集整理的python爬虫requests json与字典对象互相转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stack布局
- 下一篇: 能用条件注释改善的IE兼容问题