def request_comments(): #request comments fp = open("res.txt", "a") xw = ExcelWriter("res.xls") xw.add_sheet("test") # write head write_head(xw) polled_num = 0 current_page = 1 comment_count = 1 while(True): print "get %s" % current_page content = client.comments.timeline.get(page=current_page) total_number = content.total_number recv_num = len(content.comments) if recv_num == 0: print "recv_num = 0" break for comment in content.comments: commentRecord = Comment() if(comment.has_key("reply_comment")): commentRecord.is_reply = True commentRecord.reply_comment_id = comment.reply_comment.id commentRecord.reply_comment_text = comment.reply_comment.text commentRecord.reply_comment_created_time = comment.reply_comment.created_at commentRecord.reply_comment_user_id = comment.reply_comment.user.id commentRecord.reply_comment_user_name = comment.reply_comment.user.name commentRecord.comment_id = comment.id commentRecord.comment_text = comment.text commentRecord.comment_created_time = comment.created_at commentRecord.comment_user_id = comment.user.id commentRecord.comment_user_name = comment.user.name commentRecord.weibo_id = comment.status.id commentRecord.weibo_text = comment.status.text commentRecord.weibo_user_id = comment.status.user.id commentRecord.weibo_user_name = comment.status.user.name fp.writelines(commentRecord.vars_to_str()+'\n') # print commentRecord.is_reply, comment_count if commentRecord.is_reply == True: write_comment_xls(commentRecord, raw=comment_count, xw=xw) comment_count += 1 polled_num += recv_num print total_number, polled_num, current_page if(polled_num < total_number): current_page += 1 else: break xw.save() fp.close()