新浪微博Python SDK笔记——获取粉丝列表或关注列表

上一节中创建了一个initclient包,封装了授权的过程,通过获取的myAPIClient对象可以直接调用API接口进行微博操作,上一节中就调用了发微博的接口发了一条新微博。这一节还是直接使用initclient包,调用获取关注好友或粉丝的API来获取好友数据,并将实现的获取好友信息的功能封装在getfriends.py中,然后实现了main.py调用了其中的接口,获取了好友信息并打印出来,运行的结果如图1所示。

 

图1

【说明】:

(1)可以看出,授权过程跟上一节完全一样,是从文件中读取的上一次成功授权的access_token,没有重新进行授权;

(2)以列表的形式输出了好友的几项主要信息:uid,性别,屏幕名称和个人描述。

下面看一下getfriends.py的源码:

#! /usr/bin/python
import time
PAGE_SIZE = 200

def print_users_list(ul):
        """
        打印用户列表的详细信息
        """
	index = 0
	for user in ul:
		uid = user["id"]
		ugen = user["gender"]
		uname = user["screen_name"]
#		uloc = user["location"]
		udesc = user["description"]
		print "%-6d%-12d%-3s%s%s" % (index, uid, ugen, uname.ljust(20), udesc.ljust(40))
		index += 1
	
def get_friends(client, uid=None, maxlen=0):
        """
        读取uid用户的关注用户列表,默认uid=None,此时uid赋值为client.uid,而client.uid表示的是当前授权用户的uid.
        """
	if not uid:
		uid = client.uid
	return get_users(client, False, uid, maxlen)
	
def get_followers(client, uid=None, maxlen=0):
        """
        读取uid用户的粉丝列表,默认uid=None,此时uid赋值为client.uid,而client.uid表示的是当前授权用户的uid.
        """
	if not uid:
		uid = client.uid
	return get_users(client, True, uid, maxlen)
	
def get_users(client, followersorfriends, uid, maxlen):
        """
        调用API读取uid用户的关注用户列表或者粉丝列表,followersorfriends为True读取粉丝列表,为False读取关注好友列表,
        参数maxlen设置要获取的好友列表的最大长度,为0表示没有设置最大长度,此时会尝试读取整个好友列表,但是API对于读取的
        好友列表的长度会有限制,测试等级最大只能获取一个用户的5000条好友信息。
        """
	fl = []
	next_cursor = 0
	while True:
		if followersorfriends:
			raw_fl = client.friendships.followers.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)
		else:
			raw_fl = client.friendships.friends.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)
		fl.extend(raw_fl["users"])
		next_cursor = raw_fl["next_cursor"]
		if not next_cursor:
			break
		if maxlen and len(fl) >= maxlen:
			break
		time.sleep(1)
	return fl

【说明】:

(1)该模块中一共4个函数,get_friends和get_followers分别为获取关注好友和粉丝的接口,print_users_list为打印好友详细信息的接口。get_users为调用API的实现函数;

(2)获取关注好友和获取粉丝的API非常相似,前者是client.friendships.friends.get,后者是client.friendships.followers.get,所以我二者封装在get_users函数中,通过参数followersorfriends表示要获取的类型,接口get_followers和get_friends调用该函数,分别传入True和False;

(3)get_users函数的最后一个参数maxlen设置要获取的好友列表的最大长度,因为用户的好友列表有可能非常长,不需要全部获取时就可以设置maxlen。如果maxlen为0表示没有设置最大长度,此时会尝试读取整个好友列表,为什么说是“尝试”呢?因为API对于好友列表的获取时有限制的,测试等级最大只能获取一个用户的5000条好友信息(这个在官方文档中有说明);

(4)调用API时有一个参数count=PAGE_SIZE,因为该API读取好友列表时是按页读取的,该参数表示页的大小(即好友信息条数),默认为50,最大为200,这里设置为200,这些官方文档中都有详细说明;

(5)调用API时有一个参数cursor,表示要读取下一页的首条记录序号,每成功读取一页数据,数据中都包含了next_cursor属性,下次继续读取时从next_cursor开始读,关于cursor的详细情况可以阅读官方的API手册;

(6)print_users_list打印好友详细信息,其实详细信息项目非常的多,这里只是打印了其中的几项,可以根据自己的需要修改。

 

下面看一下main.py的源代码:

#! /usr/bin/python

from initclient import initclient
import getfriends

APP_KEY = '2024******'
APP_SECRET = '91a57*************************'
CALLBACK = 'http://bingbingrobot.sinaapp.com/'

def main():
	client = initclient.get_client(APP_KEY, APP_SECRET, CALLBACK)
	print "===> creat client successfully, authorised uid is : ", client.uid
	print "============================== users ==========================="
        #读取当前授权用户的粉丝列表
	fl = getfriends.get_followers(client)
	
	#读取当前授权用户的关注好友列表
#	fl = getfriends.get_friends(client)

        #读取uid为‘1497035431’的用户的粉丝列表
        #uid = '1497035431'	#梁斌penny
#	fl = getfriends.get_followers(client, uid=uid)

	getfriends.print_users_list(fl)


if __name__ == '__main__':
	main()

【说明】:

有了授权模块initclient和获取好友模块getfriends.py,main.py的内容就非常简单了。创建myAPIClient对象,调用getfriends模块的get_friends或get_followers接口获取好友数据,然后调用print_users_lis接口打印。非常简单,不在啰嗦了!

 

【总结】:

这两节中创建的initclient模块和getfriends模块都可以复用,开发其他功能时都可以直接使用,例如要对好友信息进行统计(如统计男女比例、兴趣分布、地区分布等)时,先用getfriends模块获取好友列表。

 

By:

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值