赞
踩
在文章《Python:通过命令行发送新浪微博》中有朋友多次留言咨询用户粉丝列表获取的方法,本来不打算在写这方面的东东,但出于程序员的特有的执着,还是写一了一下。这位朋友提供了一个链接点击打开链接,其中指定了weiapi(python版本的一个缺陷),参考其先修改了下API,改后如下:
parsers.py中ModelParser类的parse方法,如果你的和下面不一样,请参考修改。
class ModelParser(JSONParser): def __init__(self, model_factory=None): JSONParser.__init__(self) self.model_factory = model_factory or ModelFactory def parse(self, method, payload): try: if method.payload_type is None: return model = getattr(self.model_factory, method.payload_type) except AttributeError: raise WeibopError('No model for this payload type: %s' % method.payload_type) json = JSONParser.parse(self, method, payload) if isinstance(json, tuple): json, cursors = json elif isinstance(json, dict): if 'next_cursor' in json: cursors = json['next_cursor'] else: cursors = None else: cursors = None if method.payload_list: result = model.parse_list(method.api, json) else: result = model.parse(method.api, json) if cursors: return result, cursors else: return result
3、实现代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- from weibopy.auth import OAuthHandler from weibopy.api import API import ConfigParser import time MAX_PIC_NUM = 5 SLEEP_TIME_LONG = 30 def press_sina_weibo(): ''' 调用新浪微博Open Api实现通过命令行写博文,功能有待完善 author: socrates date:2012-02-06 新浪微博:@没耳朵的羊 ''' sina_weibo_config = ConfigParser.ConfigParser() #读取appkey相关配置文件 try: sina_weibo_config.readfp(open('sina_weibo_config.ini')) except ConfigParser.Error: print 'read sina_weibo_config.ini failed.' #获取需要的信息 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") #调用新浪微博OpenApi(python版) auth = OAuthHandler(consumer_key, consumer_secret) auth.setToken(token, token_sercet) api = API(auth) return api; #通过命令行输入要发布的内容 # weibo_content = raw_input('Please input content:') # status = api.update_status(status=weibo_content) # print "Press sina weibo successful, content is: %s" % status.text # iNum = 0 # while True: # #上传图片,名称和内容如果重复,open api会检查,内容采用了取当前时间的机制 # #图片名称从0-5循环遍历 # status = api.upload(str(iNum)+ '.jpg', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) # time.sleep(SLEEP_TIME_LONG) # # if iNum == MAX_PIC_NUM: # iNum = 0 # else: # iNum += 1 def get_friends(api, user_id): ''' Function:获取关注的用户列表 Input:api user_id:指定用户的ID Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-14 ''' print 'friends list: ' total_friends = 0 next_cursor = -1 while next_cursor != 0: timeline = api.friends(user_id,'','','',next_cursor) if isinstance(timeline, tuple): next_cursor = timeline[1] total_friends += len(timeline[0]) for line in timeline[0]: fid = line.__getattribute__("id") name = line.__getattribute__("screen_name") text = "friends---"+ str(fid) +":"+ name text = text.encode("gbk") print text else: next_cursor = 0 total_friends += len(timeline) for line in timeline: fid = line.__getattribute__("id") name = line.__getattribute__("screen_name") text = "friends---"+ str(fid) +":"+ name text = text.encode("gbk") print text print 'Total friends: %d' % total_friends def get_followers(api, user_id): ''' Function:获取用户的粉丝 Input:api user_id:指定用户的ID Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-14 ''' print 'followers list: ' total_friends = 0 next_cursor = -1 while next_cursor != 0: timeline = api.followers(user_id,'','','',next_cursor) if isinstance(timeline, tuple): next_cursor = timeline[1] total_friends += len(timeline[0]) for line in timeline[0]: fid = line.__getattribute__("id") name = line.__getattribute__("screen_name") text = "followers---"+ str(fid) +":"+ name text = text.encode("gbk") print text else: next_cursor = 0 total_friends += len(timeline) for line in timeline: fid = line.__getattribute__("id") name = line.__getattribute__("screen_name") text = "followers---"+ str(fid) +":"+ name text = text.encode("gbk") print text print 'Total followers: %d' % total_friends def main(): #获取关注列表 get_friends(press_sina_weibo(), 2601091753) #获取粉丝 get_followers(press_sina_weibo(), 2601091753) if __name__ == '__main__': main()
,测试不出翻页的效果,测试了下别人的,显示正常。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。