zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    鼠标滑过,解决ul下 li下a的背景与父级Li不同宽的问题
    php函数
    常用函数之数组函数
    php流程控制
    php运算符
    php常量
    php变量的数据类型
    PHP是什么
    css3新增属性
    html5的常用标签
  • 原文地址:https://www.cnblogs.com/hedianzhan/p/9613291.html
Copyright © 2011-2022 走看看