Example of drawing a friend relationship graph for Renren using python

Source: Internet
Author: User
This article describes how to use python to draw a friend relationship graph for Renren. if you need a friend, refer to the code dependency: networkx matplotlib.

The code is as follows:


#! /Bin/env python
#-*-Coding: UTF-8 -*-

Import urllib
Import urllib2
Import cookielib
Import re
Import cPickle as p
Import networkx as nx
Import matplotlib. pyplot as plt

_ Author _ = "" Reverland (lhtlyy@gmail.com )"""

# Control parameters, EDIT it here
# Login
Username = 'none'
Password = 'none'
# Control Graphs, Edit for better graphs as you need
Label_flag = True # Whether shows labels. NOTE: configure your matplotlibrc for Chinese characters.
Remove_isolated = True # Whether remove isolated nodes (less than iso_level connects)
Different_size = True # Nodes for different size, bigger means more shared friends
Iso_level = 10
Node_size = 40 # Default node size


Def login (username, password ):
"" Log in and return uid """
Logpage = "http://www.renren.com/ajaxLogin/login"
Data = {'email ': username, 'password': password}
Login_data = urllib. urlencode (data)
Cj = cookielib. CookieJar ()
Opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cj ))
Urllib2.install _ opener (opener)
Res = opener. open (logpage, login_data)
Print "Login now ..."
Html = res. read ()
# Print html

# Get uid
Print "Getting user id of you now"
Res = urllib2.urlopen ("http://www.renren.com/home ")
Html = res. read ()
# Print html
Uid = re. search ("'ruid': '(\ d +)'", html). group (1)
# Print uid
Print "Login and got uid successfully"
Return uid


Def getfriends (uid ):
"Get the uid's friends and return the dict with uid as key, name as value ."""
Print "Get % s's friend list" % str (uid)
Pagenum = 0
Dict1 = {}
While True:
Targetpage = "http://friend.renren.com/GetFriendList.do? Curpage = "+ str (pagenum) +" & id = "+ str (uid)
Res = urllib2.urlopen (targetpage)
Html = res. read ()

Pattern =''

M = re. findall (pattern, html)
# Print len (m)
If len (m) = 0:
Break
For I in range (0, len (m )):
No = m [I] [0]
Uname = m [I] [1]
# Print uname, no
Dict1 [no] = uname
Pagenum + = 1
Print "Got % s's friends list successfully." % str (uid)
Return dict1


Def getdict (uid ):
"Cache dict of uid in the disk ."""
Try:
With open (str (uid) + '.txt ', 'r') as f:
Dict_uid = p. load (f)
Except t:
With open (str (uid) + '.txt ', 'w') as f:
P. dump (getfriends (uid), f)
Dict_uid = getdict (uid)
Return dict_uid


Def getrelations (uid1, uid2 ):
"Receive two user id, If they are friends, return 1, otherwise 0 ."""
Dict_uid1 = getdict (uid1)
If uid2 in dict_uid1:
Return 1
Else:
Return 0


Def getgraph (username, password ):
"Get the Graph Object and return it.
You must specify a Chinese font such as 'simhei' in ~ /. Matplotlib/matplotlibrc """
Uid = login (username, password)
Dict_root = getdict (uid) # Get root tree

G = nx. Graph () # Create a Graph object
For uid1, uname1 in dict_root.items ():
# Encode Chinese characters for matplotlib ** IMPORTANT **
# If you want to draw Chinese labels,
Uname1 = unicode (uname1, 'utf8 ')
G. add_node (uname1)
For uid2, uname2 in dict_root.items ():
Uname2 = unicode (uname2, 'utf8 ')
# Not necessary for networkx
If uid2 = uid1:
Continue
If getrelations (uid1, uid2 ):
G. add_edge (uname1, uname2)

Return G


Def draw_graph (username, password, filename='graph.txt ', label_flag = True, remove_isolated = True, different_size = True, iso_level = 10, node_size = 40 ):
"Reading data from file and draw the graph. If not exists, create the file and re-scratch data from net """
Print "Generating graph ..."
Try:
With open (filename, 'r') as f:
G = p. load (f)
Except t:
G = getgraph (username, password)
With open (filename, 'w') as f:
P. dump (G, f)
# Nx. draw (G)
# Judge whether remove the isolated point from graph
If remove_isolated is True:
H = nx. empty_graph ()
For SG in nx. connected_component_subgraphs (G ):
If SG. number_of_nodes ()> iso_level:
H = nx. union (SG, H)
G = H
# Ajust graph for better presentation
If different_size is True:
L = nx. degree (G)
G. dot_size = {}
For k, v in L. items ():
G. dot_size [k] = v
Node_size = [G. dot_size [v] * 10 for v in G]
Pos = nx. spring_layout (G, iterations = 50)
Nx. draw_networkx_edges (G, pos, alpha = 0.2)
Nx. draw_networkx_nodes (G, pos, node_size = node_size, node_color = 'R', alpha = 0.3)
# Judge whether shows label
If label_flag is True:
Nx. draw_networkx_labels (G, pos, alpha = 0.5)
# Nx. draw_graphviz (G)
Plt. show ()

Return G

If _ name _ = "_ main __":
G = draw_graph (username, password)

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.