Use of Sina Weibo API Python

Source: Internet
Author: User

This document documents the development process using the Sina Weibo official Python SDK call API. preparatory work

Apply to be a developer and create an app:

First of all to have a Sina Weibo account, and then to the Sina Weibo open platform (http://open.weibo.com/) to create an application, the specific steps of the official website document is very detailed: Click on the developer page " Login, create app, select your app type (for example, web App), and fill out the application information.

P.S., the application needs to set a callback address (call_back), can be set as the default callback URL:https://api.weibo.com/oauth2/default.html.

When this is done, you will get three useful information: App_key,app_secret and Call_back, which will be used when you write the program later in this three message.

OAuth2.0 Authorization

Weibo open interface calls, such as tweets, concerns, etc., are required to obtain user authentication. At present, the user identity authentication of Weibo open platform is mainly based on OAuth2.0. The official documentation provides a very detailed description of the process by which the authorization mechanism helps to write programs using the SDK.

Download and install the Sina Weibo python SDK

Download The official Sina Weibo python SDK, The Web page describes the installation in two ways, the first with the Command line tool PIP installation, the second download source package installation.

For the first installation, Pip is a Python package management tool that makes it easy to install Python modules. Once the installation is successful, you only need to execute the PIP install SINAWEIBOPY,SDK package sinaweibopy on the command line (or Linux terminal) to complete the installation. To test whether the installation was successful, you can enter it on the Python command line: Import Weibo if there is no error indicating that the installation was successful. The Python SDK is essentially the module Weibo, which is called after the SDK is used.

For the second way, you need to copy the downloaded source to the installation directory or configure the path of the Python module search.

Read and write micro-blogging interface program

Below you can call the microblogging API to write a microblog operation, the following is a brief introduction of a crawl micro-blog data program.

#_*_ coding:utf-8 _*_Importsysreload (SYS) sys.setdefaultencoding ('Utf-8') fromWeiboImportapiclientImportJSONImportWebBrowserImportIoapp_key="' ## Fill in Application informationApp_secret ="'Callback_url='https://api.weibo.com/oauth2/default.html'Client= Apiclient (App_key=app_key, App_secret=app_secret, redirect_uri=callback_url) URL=Client.get_authorize_url ()#todo:redirect to URLwebbrowser.open_new (URL)#Print URL#obtain URL code:Code = raw_input ("input the code:"). Strip () client= Apiclient (App_key=app_key, App_secret=app_secret, redirect_uri=Callback_url) R=Client.request_access_token (code) Access_token= R.access_token#the token returned by Sina, similar to abc123xyz456expires_in = r.expires_in#token expires in UNIX time: Http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4#TODO: You can save access tokens hereClient.set_access_token (Access_token, expires_in) Res= Client.statuses.public_timeline.get (count=200)##返回最新的200条热门微博#res = Client.statuses.user_timeline.get () # #返回作者发布的历史微博#res = Client.statuses.friends_timeline.ids.get () # #返回好友发布的微博id#Res=client.emotions.get ()a= Json.dumps (res, ensure_ascii = False,indent = 2) Fout= Io.open ('Test','W', encoding='Utf-8') Fout.write (a)

Run the program, automatically open the callback page, the URL in the "Code" copy to the program, the program executes successfully, you can get the micro-blog data.

This is actually a call to the API provided by Sina Weibo, specific reference to official documents.

Use of Sina Weibo API Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.