1. 登陆自己的人人网,进入到网页 http://friend.renren.com/myfriendlistx.do#item_0 ,得到如下图的页面:
右键查看源代码,将下图中自己好友列表的一行复制出来,保存为 myFriends.dat (保存为一行的文本)。
2. 运行下图的源代码:
import urllib,urllib2,cookielib
import re
def 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 friendList
def Login(t,JSESSIONID):
cookie={'t':t,'JSESSIONID':JSESSIONID}
cookies=''.join(x+'='+cookie[x]+';' for x in cookie)
return cookies
def 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 friends
def 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