爬虫類のrequests,jsonの使用------jsonモジュールの新しい認知

1526 ワード

import json
import requests
"""
https://m.douban.com/rexxar/api/v2/subject_collection/tv_american/items?os=ios&for_mobile=1&callback=jsonp1&start=0&count=18&loc_id=108288&_=0
           url  ,     callback=jsonp1    ,             json  ,           json  ,        ,  
"""

url = "https://m.douban.com/rexxar/api/v2/subject_collection/tv_american/items?os=ios&for_mobile=1&start=0&count=18&loc_id=108288&_=0"

headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
    "Referer": "https://m.douban.com/tv/american"
}


response = requests.get(url, headers=headers)
# print(response.text)                 
# print(json.loads(response.text))                     

# 1.               ,    python    ,        json   ,          ,        

with open("json_douban.txt", "w", encoding="utf-8") as f:
    f.write(json.loads(response.text))

# 2.                json         ,                 ,   
# with open("json_douban_dumps.txt", "w", encoding="utf-8") as f:
#     f.write(json.dumps(json.loads(response.text), ensure_ascii=False, indent=2))

#        
# json.dumps(json.loads(response.text), ensure_ascii=False, indent=2)
#   1: ensure_ascii=False   -->   python       ,      
#   2: indent=2    -->    python