An example of Python using the sina weibo API to send tweets

  • 2020-04-02 13:33:34
  • OfStack

1. Register a sina application, get the appkey, secret, and token, and write these information to the configuration file sina_weibo_config.ini, as follows:


[userinfo]
CONSUMER_KEY=8888888888
CONSUMER_SECRET=777777f3feab026050df37d711200000
TOKEN=2a21b19910af7a4b1962ad6ef9999999
TOKEN_SECRET=47e2fdb0b0ac983241b0caaf45555555


2. Call Open Api of sina weibo, code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from weibopy.auth import OAuthHandler
from weibopy.api import API
import ConfigParser
def press_sina_weibo():
    '''
     Call sina weibo Open Api Through the command line to write blog, function to be improved 
    author: socrates
    date:2012-02-06
     Sina weibo: @ Sheep without ears 
    '''
    sina_weibo_config = ConfigParser.ConfigParser()
    # read appkey Related configuration files 
    try:
        sina_weibo_config.readfp(open('sina_weibo_config.ini'))
    except ConfigParser.Error:
        print 'read sina_weibo_config.ini failed.'
    # Get the information you need 
    consumer_key = sina_weibo_config.get("userinfo","CONSUMER_KEY")
    consumer_secret =sina_weibo_config.get("userinfo","CONSUMER_SECRET")
    token = sina_weibo_config.get("userinfo","TOKEN")
    token_sercet = sina_weibo_config.get("userinfo","TOKEN_SECRET")
    # Call sina weibo OpenApi(python version )
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.setToken(token, token_sercet)
    api = API(auth)
    # Enter what you want to publish from the command line 
    weibo_content = raw_input('Please input content:')
    status = api.update_status(status=weibo_content)
    print "Press sina weibo successful, content is: %s" % status.text
if __name__ == '__main__':
    press_sina_weibo()

3. Operation effect:

Command line input: < img border = 0 id = theimg onclick = window. The open (enclosing SRC) SRC = "/ / files.jb51.net/file_images/article/201404/2014410114253731.png? 201431011438 ">
4. Successful effect of weibo:
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201404/2014410114334718.png? 2014310114351 ">  


Related articles: