获取网络数据并保存为json文件

请求接口:https://www.apiopen.top/satinApi?type=1&page=1 获取网络数据。

将内容中所有的name和text对应的值取出,并且保存到一个json文件中,保存的格式:
[{“name”:”张三”, “text”:”哈哈,让我们一起自由的飞翔”}, {“name”:”喒你家玻璃”, “text”:”截图暂停,截到的将会是对你爱情的预言三词!”}]

数据框架:
{"code":200,"msg":"成功!","data":[{"type":"10","text":"我大中华的旗袍就是美,姐夫们,用一句话来表达你心中对妹子的赞美吧","user_id":"19837434","name":"蛮夷阿
涂","screen_name":"蛮夷阿涂","profile_image":"http://wimg.spriteapp.cn/profile/large/2017/03/10/58c2180891031_mini.jpg","created_at":"2017-09-09 12:52:02","create_time":null,"passtime":"2017-09-09 12:52:02","love":"1264","hate":"110","comment":"620","repost":"40","bookmark":"450","bimageuri":"","voiceuri":null,"voicetime":null,"voicelength":null,"status":"4","theme_id":"54779","theme_name":"性感",

#方法1(正则)
import  requests
import  re
import  json
url='https://www.apiopen.top/satinApi?type=1&page=1 '
response = requests.get(url)
data = response.json()
data = str(data['data'])

pattern1 = re.compile(r"'name':(.*?)'screen_name'",re.S)
name = re.findall(pattern1,data)


pattern2 = re.compile(r"'text':(.*?)'user_id'",re.S)
text = re.findall(pattern2,data)

new_list=[]

for i in range(len(name)):
    new_dict={}
    new_dict['name'] = name[i]
    new_dict['text'] = text[i]
    i += 1
    new_list.append(new_dict)
with open ('data1.json' ,'w',encoding='utf-8') as f:
    json.dump(new_list,f)
print(new_list)


#方法2(列表字典操作)


import  requests
import  json
url='https://www.apiopen.top/satinApi?type=1&page=1 '
response = requests.get(url)
data = response.json()
new_list = []


for data in data['data']:

    text = data['text']
    name = data['name']
    new_dict={}
    new_dict['name'] = name
    new_dict['text'] = text
    new_list.append(new_dict)

with open ('data.json' ,'w',encoding='utf-8') as f:
    json.dump(new_list,f)

print(new_list)



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,384评论 0 17
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,933评论 0 13
  • pm2 是一个带有负载均衡功能的Node应用的进程管理器。 一、概述 PM2是一个带有负载均衡功能的Node.js...
    小纠结在简书阅读 2,113评论 0 1
  • 清晨静谧的时光 阳光撒入了窗台 眼前一亮如星芒 遥想初见的一面 灯火阑珊在对岸 身影随微光闪烁 使人温暖还慵懒 如...
    陈家仁阅读 373评论 0 0

友情链接更多精彩内容