1. 登陸自己的人人網,進入到網頁 http://friend.renren.com/myfriendlistx.do#item_0 ,得到如的頁面:
右鍵查看原始碼,將中自己好友名單的一行複製出來,儲存為 myFriends.dat (儲存為一行的文本)。
2. 啟動並執行原始碼:
import urllib,urllib2,cookielibimport redef MyFriends(filename): patt2='"id":(\d+),"vip":[\s\S]+?"name":"([\s\S]+?)",\"head":"([\s\S]+?)","groups":\["([\s\S]+?)"\]' #id,name,head,groups patt='"id":(\d+),' f=open(filename,'r') for eachLine in f: friendList=re.findall(patt,eachLine) return friendListdef Login(t,JSESSIONID): cookie={'t':t,'JSESSIONID':JSESSIONID} cookies=''.join(x+'='+cookie[x]+';' for x in cookie) return cookiesdef ShareFriends(cookies,friendID): opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(\ cookielib.CookieJar())) urllib2.install_opener(opener) req = urllib2.Request('http://friend.renren.com/shareFriends?\t=0.8142646627966315&p={"init":true,"uid":true,"uhead":true,\"uname":true,"group":true,"net":true,\"param":{"guest":'+friendID+'}}') req.add_header('Cookie', cookies) content = urllib2.urlopen(req).read() patt='"id":(\d+),"netName":"' friends=re.findall(patt,content) return friendsdef AllFriendsLinkCSV(cookies,friendList,filename): f=open(filename,'w') f.write('Source,Target\n') for friendID in friendList: f.write('xxxx,'+friendID+'\n') # replace XXXX with your ID for friendID in friendList: friends=ShareFriends(cookies,friendID) for ID in friends: f.write(friendID+','+ID+'\n') f.close() def main(): friendList=MyFriends('myFriends.data2') t,JSESSIONID='xxxxx','xxxxx' cookies=Login(t,JSESSIONID) AllFriendsLinkCSV(cookies,friendList,'FriendsLink.csv') if __name__=='__main__': main()
函數 MyFriends 用於擷取自己的好友名單
ShareFriends 用於擷取和某個好友的共同好友
AllFriendsLinkCSV 按照Gephi的指定格式將好友關係儲存為csv檔案。
main函數中的t,JSESSIONID通過如下方式擷取後,取代上面代碼中的xxxxx:
在人人頁面中右鍵,審查元素,資源選項下的紅色覆蓋部分
3. 替換t和JSESSIONID值後運行上述代碼,得到 FriendsLink.csv。按照在Gephi中匯入csv檔案:
回到概覽就可以看見網狀圖:
經過一系列處理後就可以得到自己的社交圖啦~
參考文獻:
[1] 人人cookie登陸: http://www.guokr.com/post/317472/
[2] Gephi中文教程: https://www.udemy.com/gephi
[3] Coursera 上的Social network analysis課程: https://www.coursera.org/course/sna