gephi和python_利用python、Gephi绘制人人的社交网络图 | 学步园

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值