新浪微博API认证并采集最新微博等信息

来源:互联网 发布:尚学堂大数据课程 编辑:程序博客网 时间:2024/04/29 08:45
# -*-coding:utf-8-*-'''@ Weibo API公共接口@ Date : 12/25/2014'''from weibo import APIClientimport json,webbrowserimport sysreload(sys)sys.setdefaultencoding('utf8')# 定义全局变量APP_KEY      = '18******23'# 应用IDAPP_SECRET   = 'cff***********f0************67c'# 应用的密钥CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html'# 回调地址class WeiboAPI():def __init__(self,access_token=None):global APP_KEYglobal APP_SECRETglobal CALLBACK_URLself.client = APIClient(APP_KEY,APP_SECRET,CALLBACK_URL)self.access_token = access_token# OAuth2.0认证过程def OAuth_regisit(self):# 输入code,access_token,完成认证url = self.client.get_authorize_url()webbrowser.open_new(url)code = raw_input("Please input the code: ")# 手动输入回调链接url中的codeprint code# code = '0371ba3ea9db656a568de81f468a7982'request = self.client.request_access_token(code,CALLBACK_URL)self.access_token = request.access_token# 获得tokenexpires_in = request.expires_in# 授权的过期时间print self.access_tokenprint "Weibo OAuth2.0 Regisit Sucessful!"self.client.set_access_token(self.access_token,expires_in)def public_timeline(self):# 获取最新的200条公共微博public_data = self.client.statuses.public_timeline.get(count=200)# public_data is JsonDictself.weibo_parse(public_data)# 微博self.user_parse(public_data)# 用户# 解析微博def weibo_parse(self,weibocontent):if weibocontent:total_number = int(weibocontent['total_number'])# 总条数print "The Total Numbers: ",total_numberstatuses = weibocontent['statuses']# 内容块for item in statuses:userid = item['user']['id']# 微博作者UIDid = item['id']# 微博唯一idtext = item['text'].encode("utf8","ignore")# 微博信息内容weibo_created_at = item['created_at']# 微博发布时间comments_count = int(item['comments_count'])# 评论数print id# 解析作者信息def user_parse(self,usercontent):if usercontent:statuses = usercontent['statuses']for item in statuses:user = item['user']#****************************************************userid = user['id']# 用户UIDgender = user['gender']# 性别screen_name = user['screen_name']# 用户昵称created_at = user['created_at']# 用户注册时间#****************************************************friends_count = user['friends_count']# 关注数followers_count = user['followers_count']# 粉丝数statuses_count = user['statuses_count']# 微博数bi_followers_count = user['bi_followers_count']# 用户的互粉数favourites_count    = user['favourites_count']# 收藏数#****************************************************location = user['location']# 用户所在地province = int(user['province'])# 用户所在省级IDcity  = user['city']# 用户所在城市IDverified = user['verified']# 是否是微博认证用户verified_reason = user['verified_reason']# 认证原因#****************************************************domain  = user['domain']# 个性化域名remark       = user['remark']# 用户的备注信息avatar_large = user['avatar_large']# 用户的头像大图description = user['description']# 用户个人描述#****************************************************print useridprofileurl = 'http://weibo.com/u/'+ str(userid)+'/'# 个人主页# 获取当前登录用户的关注列表def friends_parse(self):friends_data = self.client.friendships.friends.get(uid=5086800911,count=200)if friends_data:i = 0for item in friends_data['users']:id = item['id']i += 1print i,"=======",idif __name__ == "__main__":uid = [50******11]test = WeiboAPI()test.OAuth_regisit()test.friends_parse()# 循环从接口中获取当前时刻发布的公共微博及作者信息a = 5while a:test.public_timeline()a -= 1

0 0