#coding:utf-8
from weibo import APIClient
import webbrowser
import MySQLdb
APP_KEY = '984793585' # app key
APP_SECRET = 'ab2c926021d5cfbbc75587e67bd05a8c' # app secret
CALLBACK_URL = 'http://weibo.com/muqingcai/home?wvr=5'# callback url       


#利用官方微博SDK
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)


#用得到的url到新浪頁面訪問
url = client.get_authorize_url()
webbrowser.open_new(url)


#手動輸入新浪返回的code
code = raw_input("input the code: ").strip()


#新浪返回的token,類似abc123xyz456,每天的token不一樣
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in # token過期的UNIX時間


#設定得到的access_token
client.set_access_token(access_token, expires_in)


#有了access_token後,可以做任何事情了


#print client.statuses__public_timeline()  
count = 1
s = set([('mu','basketball','guangzhou','liuchuanfen')])  
def getDataByPublic(count):
    while True:
        if count>=50:
            break
        statuses = client.statuses__public_timeline()['statuses']  
        length = len(statuses)          
        #輸出了部分資訊  
        for i in range(0,length):  
            nickName = statuses[i]['user']['screen_name']  
            profile = statuses[i]['user']['description']  
            location = statuses[i]['user']['location']  
            weibo = statuses[i]['text'] 
            print u'暱稱:'+nickName
            print u'簡介:'+profile
            print u'位置:'+location  
            print u'微博:'+weibo  
        count += 1    
getDataByPublic(1)
.