?在文章[《?Python:通過命令行發送新浪微博》](http://blog.csdn.net/dyx1024/article/details/7237823#reply)中有朋友多次留言咨詢用戶粉絲列表獲取的方法,本來不打算在寫這方面的東東,但出于程序員的特有的執著,還是寫一了一下。這位朋友提供了一個鏈接[點擊打開鏈接](http://www.cnblogs.com/coucar/archive/2011/09/14/2176456.html),其中指定了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
~~~
2、獲取列表,提供一個你要獲取的用戶id即可,例如我的微博ID:2601091753,用戶名:沒耳朵的羊,關注用戶為71人,粉絲8人(悲催至極啊),如下:

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()
~~~
測試:
~~~
friends list:?
friends---1248584111:曹筱燊V
friends---1951657750:輕博客
friends---2264489285:Qing官方活動
friends---1650867513:365day
friends---1296492473:單車旅行的肥貓
friends---1678325381:看得見風景的記憶
friends---2129867007:國家地理攝影
friends---2179565352:地球人Echo
friends---2447164602:小克愛家居
friends---1742924093:60designwebpick
friends---2261697941:輕家居
friends---1072112375:cugala
friends---1917369903:閆璐
friends---2485697323:鏡頭中的黑白世界
friends---1400314314:源形畢露
friends---1629756430:LoveLomo
friends---2638745273:小賢d點滴
friends---1401880315:左耳朵耗子
friends---1993292930:經典經濟學
friends---2557129567:華為中國區
friends---1671248621:林正剛
friends---1670481425:伯樂在線官方微博
friends---1216265283:修羅陛下的微博
friends---2694391504:口袋英語2012
friends---1924010407:laiyonghao
friends---1784501333:數據挖掘與數據分析
friends---2340488972:BalaBala_Fiona
friends---1097438111:小洋洋的西紅柿醬
friends---1649005320:俞敏洪
friends---2640459400:讀書的旅程
friends---1879347450:西安生活情報
friends---1949305184:微博桌面
friends---1894238970:developerWorks
friends---1400220917:開源中國
friends---2093492691:程序員的那些事
friends---1746173800:InfoQ
friends---2140710271:芝雪兒
friends---1100856704:余承東
friends---1839167003:華為終端官方微博
friends---1975995305:西安晚報
friends---2202941172:陜西美食
friends---1717833412:華商報
friends---1750354524:西安交通旅游廣播
friends---1698784044:三秦都市報
friends---1642909335:微博小秘書
friends---1784599353:davewliu
friends---1801473501:徐沛欣
friends---1806762625:陳一舟
friends---1798056081:萬網張向東
friends---1839240561:魯明同學
friends---1756644751:孫陶然
friends---1497145741:楊楊楊楊楊楊楊
friends---1777984105:王微
friends---1749127163:雷軍
friends---1657288682:葉朋
friends---1764529885:唐彬
friends---1861284644:盧琪隆
friends---1781681447:求伯君
friends---1055071394:姚玨
friends---1255647187:丁守謙
friends---1662521105:PPS徐偉峰
friends---1699907747:ChinaCache王松
friends---1744303552:謝國睿
friends---1657681711:潛水員莊辰超
friends---1812591014:徐少春
friends---1759084801:暴風馮鑫
friends---1772406523:買彥州
friends---1822223433:宮玉國
friends---1768340073:沈博陽
friends---1831348402:billgates
friends---1670071920:史玉柱
Total friends: 71
followers list:?
followers---2463471483:吃貨通
followers---1097438111:小洋洋的西紅柿醬
followers---1659617193:在少有人走的路上
followers---2386093060:朵朵奇6850
followers---2340488972:BalaBala_Fiona
followers---2090442510:寧兒_YOYO
followers---2215084900:景濤濤
followers---2140710271:芝雪兒
Total followers: 8
~~~
可見,與實際數目相等,列表獲取完整,由于我的粉絲太少了
,測試不出翻頁的效果,測試了下別人的,顯示正常。
- 前言
- Python:實現文件歸檔
- Pyhon:按行輸出文件內容
- Python:讀文件和寫文件
- Python:實現一個小算法
- Python:通過命令行發送新浪微博
- Python:通過攝像頭實現的監控功能
- Python:通過攝像頭抓取圖像并自動上傳至新浪微博
- Python:簡單的攝像頭程序實現
- Python:日志模塊logging的應用
- Python:操作嵌入式數據庫SQLite
- Python:將句子中的單詞全部倒排過來,但單詞的字母順序不變
- Python:語音處理,實現在線朗讀RFC文檔或本地文本文件
- Python:通過計算階乘來學習lambda和reduce這兩個函數的使用
- Python:通過執行100萬次打印來比較C和python的性能,以及用C和python結合來解決性能問題的方法
- Python:使用matplotlib繪制圖表
- Python:使用pycha快速繪制辦公常用圖(餅圖、垂直直方圖、水平直方圖、散點圖等七種圖形)
- Python:使用pycha快速繪制辦公常用圖二(使用樣式定制個性化圖表)
- Python:監控鍵盤輸入、鼠標操作,并將捕獲到的信息記錄到文件中
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性(二)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(一)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(二)
- Python:通過自定義系統級快捷鍵來控制程序運行
- Python:通過自定義系統級快捷鍵來控制程序開始或停止記錄日志(使用小技巧解決一個貌似無解的問題)
- Python:一個多功能的抓圖工具開發(附源碼)
- Python:程序發布方式簡介一(打包為可執行文件EXE)
- Python:新浪微博應用開發簡介(認證及授權部分)
- Python:程序最小化到托盤功能實現
- Python:實用抓圖工具開發介紹(含需求分析、設計、編碼、單元測試、打包、系統測試、發布各環節)
- Python:桌面氣泡提示功能實現
- Python:未來三個月的python學習計劃
- Python:pygame模塊及SDL庫簡介
- Python:獲取新浪微博用戶的收聽列表和粉絲列表
- Python:pygame游戲編程之旅一(Hello World)
- Python:pygame游戲編程之旅二(自由移動的小球)
- Python:pygame游戲編程之旅三(玩家控制的小球)
- Python:pygame游戲編程之旅四(游戲界面文字處理)
- Python:pygame游戲編程之旅五(游戲界面文字處理詳解)
- Python:pygame游戲編程之旅六(游戲中的聲音處理)
- Python:pygame游戲編程之旅七(pygame基礎知識講解1)
- Python:編程“八榮八恥”之我見
- Python:腳本的幾種執行方式
- wxPython:簡單的wxPython程序
- wxPython:簡單的wxPython程序的另一種寫法
- wxPython:應用程序對象介紹
- wxPython:輸出重定向
- wxPython:關閉wxPython程序
- wxPython:Frame類介紹
- wxPython:面板Panel的使用
- wxPython:工具欄、狀態欄、菜單實現
- wxPython:消息對話框MessageDialog
- wxPython:文本對話框TextEntryDialog
- wxPython:列表選擇框SingleChoiceDialog
- wxPython:事件處理介紹一
- wxPython:事件處理介紹二
- wxPython: 簡單的繪圖例子
- wxPython:狀態欄介紹
- wxPython:菜單介紹
- wxPython:文件對話框wx.FileDialog
- wxPython:顏色選擇對話框wx.ColourDialog
- wxPython:布局管理器sizer介紹
- wxPython:啟動畫面SplashScreen介紹
- wxPython:繪畫按鈕BitmapButton介紹
- wxPython:進度條Gauge介紹
- Python: 發送新浪微博(使用oauth2)
- Python:讀取新浪微博收聽列表
- Python:DNS客戶端實現