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

2018-10-24  本文已影响0人  李小萌mmm

请求接口: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)



上一篇下一篇

猜你喜欢

热点阅读