首页 > 代码库 > 新浪微博数据挖掘食谱之四: 保存篇 (json text格式)

新浪微博数据挖掘食谱之四: 保存篇 (json text格式)

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-12-31
@author: beyondzhou
@name: json_data_text.py
'''

# Get public timeline of sina weibo and save json response data into text file
def json_data_text():
    
    # import 
    from login import weibo_login
    from data import save_to_text, load_from_text
        
    # Access to sina api
    weibo_api = weibo_login()
    
    # Get public timeline
    public_timeline = weibo_api.statuses.public_timeline.get(count=200)

    # Output the public timeline
    # print json.dumps(public_timeline, indent=1)
    
    # Save the json data into text file
    save_to_text('publicTimeline', public_timeline)
    
    # Read the json data from text file
    results = load_from_text('publicTimeline')
    print results
    
if __name__ == '__main__':
    json_data_text()

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-12-31
@author: beyondzhou
@name: data.py
'''

import io, json

# Save json data into text file
def save_to_text(filename, data):
    with io.open(r'e:\automation\{0}.json'.format(filename),'w', encoding='utf-8') as f:
        f.write(unicode(json.dumps(data, ensure_ascii=False, indent=1)))
        
# Load json data from text file
def load_from_text(filename):
    with io.open(r'e:\automation\{0}.json'.format(filename), encoding='utf-8') as f:
        return f.read()

Results:

     "thumbnail_pic": "http://ww2.sinaimg.cn/thumbnail/e64ab084gw1en83gcxf4dj20j20dr76e.jpg"
    }
   ], 
   "in_reply_to_user_id": "", 
   "darwin_tags": [], 
   "favorited": false, 
   "original_pic": "http://ww4.sinaimg.cn/large/e64ab084gw1en83fpbam0j20ce0ddjsn.jpg", 
   "idstr": "3793732287449186", 
   "source_type": 1, 
   "user": {
    "bi_followers_count": 5, 
    "domain": "", 
    "avatar_large": "http://tp1.sinaimg.cn/5227912516/180/5701194981/0", 
    "verified_source": "", 
    "ptype": 0, 
    "statuses_count": 2102, 
    "allow_all_comment": true, 
    "id": 5227912516, 
    "verified_reason_url": "", 
    "city": "1000", 
    "province": "100", 
    "credit_score": 80, 
    "block_app": 0, 
    "follow_me": false, 
    "verified_reason": "", 
    "followers_count": 77, 
    "location": "其他", 
    "verified_trade": "", 
    "mbtype": 0, 
    "verified_source_url": "", 
    "profile_url": "u/5227912516", 
    "block_word": 0, 
    "avatar_hd": "http://ww2.sinaimg.cn/crop.0.0.200.200.1024/005HNMRCjw1eiogrh9pubj305k05k0sn.jpg", 
    "star": 0, 
    "description": "", 
    "friends_count": 756, 
    "online_status": 0, 
    "mbrank": 0, 
    "idstr": "5227912516", 
    "profile_image_url": "http://tp1.sinaimg.cn/5227912516/50/5701194981/0", 
    "allow_all_act_msg": false, 
    "verified": false, 
    "geo_enabled": true, 
    "class": 1, 
    "screen_name": "心塞的清芬", 
    "lang": "zh-cn", 
    "weihao": "", 
    "remark": "", 
    "favourites_count": 0, 
    "name": "心塞的清芬", 
    "url": "", 
    "gender": "f", 
    "created_at": "Wed Jul 23 00:00:03 +0800 2014", 
    "verified_type": -1, 
    "following": false, 
    "pagefriends_count": 0, 
    "urank": 7
   }, 
   "geo": null, 
   "created_at": "Wed Dec 31 06:48:43 +0800 2014", 
   "mlevel": 0, 
   "comments_count": 0
  }, 
  {
   "reposts_count": 0, 
   "truncated": false, 
   "text": "[疑问] 链 接:http://t.cn/Rzsuyk4", 
   "visible": {
    "type": 0, 
    "list_id": 0
   }, 
   "in_reply_to_status_id": "", 
   "bmiddle_pic": "http://ww3.sinaimg.cn/bmiddle/e64ab084gw1en83w3opzrj20h30gpdi6.jpg", 
   "id": 3793732287449183, 
   "thumbnail_pic": "http://ww3.sinaimg.cn/thumbnail/e64ab084gw1en83w3opzrj20h30gpdi6.jpg", 
   "mid": "3793732287449183", 
   "source": "<a href=http://www.mamicode.com/"http://app.weibo.com/t/feed/63af84/" rel=/"nofollow/">vivo智能手机", >

新浪微博数据挖掘食谱之四: 保存篇 (json text格式)