zoukankan      html  css  js  c++  java
  • Python 获取新浪微博的热门话题 (API)

    Code:

    #!/usr/bin/python 
    # -*- coding: utf-8 -*-
    
    '''
    Created on 2014-06-27
    @author: guaguastd
    @name: sinaWeiboHotTrend.py
    '''
    
    import json
    from sinaWeiboLogin import sinaWeiboLogin
        
    # get weibo_api to access sina api
    sinaWeiboApi = sinaWeiboLogin()
    
    # get sinaWeiboTrend
    from sinaWeiboTrend import trendClass
    trends = trendClass(sinaWeiboApi)
    
    # get sinaWeiboTrend by hourly, by daily, by weekly
    while 1:
        while True:
            choice = raw_input("
    input choice to get sinaWeiboTrend (1 means hourly, 2 means daily, 3 means weekly, 0 to quit):")
            if choice.isdigit():
                choice = int(choice)
                if choice == 0 or choice == 1 or choice == 2 or choice == 3 :
                    break
                else:
                    print 'input error, should input int number among 0,1,2,3, try again!'
                
            else:
                print 'input error, should input int number, please try again!'
                
        if choice == 0:
            print 'Succssfully exit!
    '
            break
        elif choice == 1:
            print 'Hourly sinaWeiboTrend are as follow:
    '
            hourly_trends = trends.hourly_new_trend()
            print json.dumps(hourly_trends, indent=1)
        elif choice == 2:
            print 'Daily sinaWeiboTrend are as follow:
    '
            daily_trends = trends.daily_new_trend()
            print json.dumps(daily_trends, indent=1)
        elif choice == 3:
            print 'Weekly sinaWeiboTrend are as follow:
    '
            weekly_trends = trends.weekly_new_trend()
            print json.dumps(weekly_trends, indent=1)
        else:
            print 'Input error, try again!
    '

    RESULT:

    input choice to get sinaWeiboTrend (1 means hourly, 2 means daily, 3 means weekly, 0 to quit):1
    Hourly sinaWeiboTrend are as follow:
    {
     "trends": {
      "2014-07-06 08:16": [
       {
        "query": "u6797u5999u53ef", 
        "amount": "26088", 
        "name": "u6797u5999u53ef", 
        "delta": "26088"
       }, 
       {
        "query": "u8d1du513ffeynman", 
        "amount": "24442", 
        "name": "u8d1du513ffeynman", 
        "delta": "24442"
       }, 
       {
        "query": "u5434u901fu73b2", 
        "amount": "22423", 
        "name": "u5434u901fu73b2", 
        "delta": "22423"
       }, 
       {
        "query": "u8a00u627fu65ed", 
        "amount": "21956", 
        "name": "u8a00u627fu65ed", 
        "delta": "21956"
       }, 
       {
        "query": "u5434u5988", 
        "amount": "21533", 
        "name": "u5434u5988", 
        "delta": "21533"
       }, 
       {
        "query": "u591au59887788", 
        "amount": "21314", 
        "name": "u591au59887788", 
        "delta": "21314"
       }, 
       {
        "query": "u52a0u6cb9u5434u4ea6u51e1", 
        "amount": "21051", 
        "name": "u52a0u6cb9u5434u4ea6u51e1", 
        "delta": "21051"
       }, 
       {
        "query": "u5305u5b50u59b9", 
        "amount": "20939", 
        "name": "u5305u5b50u59b9", 
        "delta": "20939"
       }, 
       {
        "query": "u7537u795eu9e7fu6657u91cdu8fd4u5febu672c", 
        "amount": "20570", 
        "name": "u7537u795eu9e7fu6657u91cdu8fd4u5febu672c", 
        "delta": "20570"
       }, 
       {
        "query": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "amount": "20033", 
        "name": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "delta": "20033"
       }
      ]
     }, 
     "as_of": 1404605804
    }
    
    input choice to get sinaWeiboTrend (1 means hourly, 2 means daily, 3 means weekly, 0 to quit):2
    Daily sinaWeiboTrend are as follow:
    {
     "trends": {
      "2014-07-06 08:23": [
       {
        "query": "u6797u5999u53ef", 
        "amount": "26434", 
        "name": "u6797u5999u53ef", 
        "delta": "26434"
       }, 
       {
        "query": "u8d1du513ffeynman", 
        "amount": "24677", 
        "name": "u8d1du513ffeynman", 
        "delta": "24677"
       }, 
       {
        "query": "u5434u901fu73b2", 
        "amount": "22716", 
        "name": "u5434u901fu73b2", 
        "delta": "22716"
       }, 
       {
        "query": "u8a00u627fu65ed", 
        "amount": "22315", 
        "name": "u8a00u627fu65ed", 
        "delta": "22315"
       }, 
       {
        "query": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "amount": "22162", 
        "name": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "delta": "22162"
       }, 
       {
        "query": "u5434u5988", 
        "amount": "21776", 
        "name": "u5434u5988", 
        "delta": "21776"
       }, 
       {
        "query": "u52a0u6cb9u5434u4ea6u51e1", 
        "amount": "21389", 
        "name": "u52a0u6cb9u5434u4ea6u51e1", 
        "delta": "21389"
       }, 
       {
        "query": "u5305u5b50u59b9", 
        "amount": "21232", 
        "name": "u5305u5b50u59b9", 
        "delta": "21232"
       }, 
       {
        "query": "u53e4u4e3du7c73u5a1c", 
        "amount": "19406", 
        "name": "u53e4u4e3du7c73u5a1c", 
        "delta": "19406"
       }, 
       {
        "query": "u91d1u661f", 
        "amount": "19130", 
        "name": "u91d1u661f", 
        "delta": "19130"
       }
      ]
     }, 
     "as_of": 1404606238
    }
    
    input choice to get sinaWeiboTrend (1 means hourly, 2 means daily, 3 means weekly, 0 to quit):3
    Weekly sinaWeiboTrend are as follow:
    {
     "trends": {
      "2014-07-06 08:24": [
       {
        "query": "u6797u5999u53ef", 
        "amount": "26434", 
        "name": "u6797u5999u53ef", 
        "delta": "26434"
       }, 
       {
        "query": "u8d1du513ffeynman", 
        "amount": "24677", 
        "name": "u8d1du513ffeynman", 
        "delta": "24677"
       }, 
       {
        "query": "u5434u901fu73b2", 
        "amount": "22716", 
        "name": "u5434u901fu73b2", 
        "delta": "22716"
       }, 
       {
        "query": "u8a00u627fu65ed", 
        "amount": "22315", 
        "name": "u8a00u627fu65ed", 
        "delta": "22315"
       }, 
       {
        "query": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "amount": "22162", 
        "name": "u8377u5170 u54e5u65afu8fbeu9eceu52a0", 
        "delta": "22162"
       }, 
       {
        "query": "u5434u5988", 
        "amount": "21776", 
        "name": "u5434u5988", 
        "delta": "21776"
       }, 
       {
        "query": "u52a0u6cb9u5434u4ea6u51e1", 
        "amount": "21389", 
        "name": "u52a0u6cb9u5434u4ea6u51e1", 
        "delta": "21389"
       }, 
       {
        "query": "u5305u5b50u59b9", 
        "amount": "21232", 
        "name": "u5305u5b50u59b9", 
        "delta": "21232"
       }, 
       {
        "query": "u53e4u4e3du7c73u5a1c", 
        "amount": "19406", 
        "name": "u53e4u4e3du7c73u5a1c", 
        "delta": "19406"
       }, 
       {
        "query": "u91d1u661f", 
        "amount": "19130", 
        "name": "u91d1u661f", 
        "delta": "19130"
       }
      ]
     }, 
     "as_of": 1404606244
    }
    
    input choice to get sinaWeiboTrend (1 means hourly, 2 means daily, 3 means weekly, 0 to quit):

  • 相关阅读:
    文件传输基础——Java IO流
    Oracle数据库之PL/SQL基础
    Oracle数据库之SQL基础(二)
    Oracle数据库之SQL基础(一)
    jQuery基础修炼圣典—DOM篇(二)jQuery遍历
    jQuery基础修炼圣典—DOM篇(一)
    Java静态代码分析工具——FindBugs插件的安装与使用
    javascript:void(0)和javascript:;的用法
    MySQL之数据类型与操作数据表
    Java数据库连接——JDBC调用存储过程,事务管理和高级应用
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7147881.html
Copyright © 2011-2022 走看看