請求接口:https://www.apiopen.top/satinApi?type=1&page=1 獲取網(wǎng)絡數(shù)據(jù)肩碟。
將內(nèi)容中所有的name和text對應的值取出艺蝴,并且保存到一個json文件中,保存的格式:
[{“name”:”張三”, “text”:”哈哈际起,讓我們一起自由的飛翔”}, {“name”:”喒你家玻璃”, “text”:”截圖暫停,截到的將會是對你愛情的預言三詞吐葱!”}]
數(shù)據(jù)框架:
{"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)