Python calls Sina Weibo API crawl user's buddy list (Windows 64 bit, Pycharm) __python

Source: Internet
Author: User

Because of the need of social network analysis research, we need to get the friend list data of the designated users, so call the Sina Weibo API for practice. Although Sina Weibo open platform interface upgrade can only get the current logged-on user's partial friend list, unable to meet the research needs, but still with this record first call API crawl data experience.

1. Create an application under the Weibo open platform (http://open.weibo.com/) to get the app Key and app Secret


2. Under Application information, in the Advanced Information section, set the authorization callback page to: https://api.weibo.com/oauth2/default.html, and set the cancel authorization callback page to: https://api.weibo.com /oauth2/default.html.


3. Download the Sina Weibo python SDK (http://github.liaoxuefeng.com/sinaweibopy/) and extract it into the current folder Sinaweibopy.

4. Open Pycharm, create a new project crawl, and create a new Python File named Weibo_ api.py, at the same time, open workspace, copy the weibo.py under the Sinaweibopy folder to the workspace folder under crawl.


5. The Python code to crawl the buddy list is as follows.

#!/usr/bin/env python
#-*-coding:utf-8-*-
apiclient
webbrowser# Python's built-in package

App_key = ' ************* ' #注意替换这里为自己申请的App信息
app_secret = ' **************************
' Callback_url = ' https://api.weibo.com/oauth2/default.html ' #回调授权页面
#利用官方微博SDK
client = apiclient (app_key= App_key, App_secret=app_secret, Redirect_uri=callback_url)
#得到授权页面的url, using WebBrowser to open this URL
URL Client.get_authorize_url ()
url
webbrowser.open_new (URL)
#获取code = subsequent content
 ' Enter the contents of the code behind the URL and press ENTER: '
code = raw_input ()
r = Client.request_access_token (code)
Access_token = R.access_token # Sina returns token, similar abc123xyz456
expires_in = r.expires_in
# settings get Access_token
client.set _access_token (Access_token, expires_in)
print ' Friends list: '
Resfollows = []
nextcursor =-1
nextcursor!= 0:
    followers = client.get.friendships__friends (uid= ' ********* ', count=200, cursor=nextcursor)
    nextcursor = followers["Next_cursor"]
      followers["users":
        FID = follower[' id ']
        name = follower[' Screen_name ']
        text = "Friends---" + str (FID) + ": "+ name
        Text = Text.encode (" Utf-8 ")
        print(text)
        resfollows.append (follower[" Screen_ Name "], follower[" gender "])

len (resfollows)
  Attention:
API interface Information Reference API documentation (Http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI), for example, in Friendships/friends, there are three ways to invoke:
         1. Client.get.friendships__friends (), replace "/" with Two "_", and use Get or post, depending on whether the request is post or get;
         2. For a Get method, the call can be omitted from the take, that is, Client.friendships__friends ()
         3. Client.friendships__friends.get (), replace "/" in the API with ".", and finally specify whether to get or post.
Friendships/friends parameter Description:
parameter UID and Screen_name both must choose one, and can only choose one;
after the interface upgrade: UID and screen_name only for the current authorized user;
Only returns users who also authorize the application, the unauthorized user will not return,
such as a call count of 50, but only 10 of the users who authorize the application will actually return 10; Use the
official Mobile SDK call to return 30% more users than the same authorized application. The total limit is 500.

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.