Use Python and gephi to draw social network diagrams for everyone

Source: Internet
Author: User

1. login to their Renren network, enter the web page http://friend.renren.com/myfriendlistx.do#item_0, get such as the page:

Right-click to view the source code, copy a line from your friends list, and save it as myfriends. dat (save as a line of text ).

2. Run the source code:

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()

The myfriends function is used to obtain a list of friends.

Mutual friends is used to obtain mutual friends with a friend.

Allfriendslinkcsv saves the friend relationship as a CSV file in the format specified by gephi.

In the main function, after obtaining the T and JSESSIONID in the following way, replace XXXXX In the above Code:

Right-click on the Renren page, review elements, and red overwrites under resource options

3. Replace the T and JSESSIONID values and run the above code to get friendslink.csv. Follow the steps below to import CSV files in gephi:

Back to the overview page, you can see the network diagram:

After a series of operations, you can get your own social graphs ~

References:

[1] Everybody cookie login: http://www.guokr.com/post/317472/

Gephi Chinese Tutorial: https://www.udemy.com/gephi

[3] Social Network Analysis Course on Coursera: https://www.coursera.org/course/sna

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.