Introduction: These days in the study of how to call the Sina open Platform API analysis of Sina Weibo user data
Results: Successfully called Sina API to get some personal information of users and no number of public Weibo
Insufficient: Sina open platform access is limited, the data returned is really limited, not enough to analyze the problem, it is necessary to analyze the problem or to have a person to write a crawler
Here is the procedure to invoke the Sina Open API:
The first step: just do it.
Http://www.cnblogs.com/dhsunny/p/3578399.html?utm_source=tuicool&utm_medium=referral
One of them is not that Sina API test tool is not open, to own Baidu
API Test Tool opens
The second step: This is my key to introduce a step, call Sina API how to get the data, can not be on the API test tool to get it, this time I use a python
First build the python development environment, specific reference: http://www.imooc.com/learn/397
Personal advice to build on eclipse, with tutorials on video
Here is the code that calls the Sina API: the interface that is called is
Statuses/public_timelineThe introduction of this interface is: Http://open.weibo.com/wiki/2/statuses/public_timelineThe code is as follows: #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, like abc123xyz456, tokens are different every day.
r = Client.request_access_token (code)
Access_token = R.access_token
expires_in = r.expires_in # token expires Unix time
#设置得到的access_token
Client.set_access_token (Access_token, expires_in)
#有了access_token后, you can do anything.
#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: ' +nickname
Print u ' profile: ' +profile
Print u ' position: ' +location
Print U ' Weibo: ' +weibo
Count + = 1
Getdatabypublic (1) Note: App_key and App_secret need to create applications on the Sina open platform to obtain, the specific method to see the first step, Callback_url also in the application of information in the high-level information, this is to return the address, fill in your Weibo home addressRun the above program: will pop up:
Enter your Weibo account password and go to the Weibo homepage
to enter code into the console press ENTER:data, which can exist in a database, or can be stored locally
How Python calls the Sina API interface problem