使用python绘制人人网好友关系图示例


Posted in Python onApril 01, 2014

代码依赖:networkx matplotlib

 #! /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 = '<a href="http://www\\.renren\\.com/profile\\.do\\?id=(\\d+)"><img src="[\\S]*" alt="[\\S]*[\\s]\\((.*)\\)" />'
        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:
        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:
        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)
Python 相关文章推荐
Python三元运算实现方法
Jan 12 Python
Python实现处理管道的方法
Jun 04 Python
Python实现小数转化为百分数的格式化输出方法示例
Sep 20 Python
python使用matplotlib画柱状图、散点图
Mar 18 Python
python实现连连看辅助(图像识别)
Mar 25 Python
python能做什么 python的含义
Oct 12 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
Mar 09 Python
Python判断三段线能否构成三角形的代码
Apr 12 Python
完美解决keras 读取多个hdf5文件进行训练的问题
Jul 01 Python
django中cookiecutter的使用教程
Dec 03 Python
Python字符串对齐、删除字符串不需要的内容以及格式化打印字符
Jan 23 Python
python 制作本地应用搜索工具
Feb 27 Python
python异步任务队列示例
Apr 01 #Python
用Python编程实现语音控制电脑
Apr 01 #Python
35个Python编程小技巧
Apr 01 #Python
ptyhon实现sitemap生成示例
Mar 30 #Python
python实现百度关键词排名查询
Mar 30 #Python
python获取网页状态码示例
Mar 30 #Python
python单线程实现多个定时器示例
Mar 30 #Python
You might like
php预定义变量使用帮助(带实例)
2013/10/30 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
javascript中运用闭包和自执行函数解决大量的全局变量问题
2010/12/30 Javascript
JavaScript中圆括号()和方括号[]的特殊用法疑问解答
2013/08/06 Javascript
js中reverse函数的用法详解
2013/12/26 Javascript
BAT及各大互联网公司2014前端笔试面试题--JavaScript篇
2014/10/29 Javascript
JS或jQuery获取ASP.NET服务器控件ID的方法
2015/06/08 Javascript
JS实现适合于后台使用的动画折叠菜单效果
2015/09/21 Javascript
全面解析Bootstrap中transition、affix的使用方法
2016/05/30 Javascript
js es6系列教程 - 基于new.target属性与es5改造es6的类语法
2017/09/02 Javascript
JS库 Highlightjs 添加代码行号的实现代码
2017/09/13 Javascript
JS实现带动画的回到顶部效果
2017/12/28 Javascript
js推箱子小游戏步骤代码解析
2018/01/10 Javascript
从源码里了解vue中的nextTick的使用
2018/11/22 Javascript
11个教程中不常被提及的JavaScript小技巧(推荐)
2019/04/17 Javascript
vue下canvas裁剪图片实例讲解
2020/04/16 Javascript
让IDE识别webpack的别名alias的实现方法
2020/05/06 Javascript
python的urllib模块显示下载进度示例
2014/01/17 Python
书单|人生苦短,你还不用python!
2017/12/29 Python
python判断设备是否联网的方法
2018/06/29 Python
Python基于matplotlib画箱体图检验异常值操作示例【附xls数据文件下载】
2019/01/07 Python
django 解决自定义序列化返回处理数据为null的问题
2020/05/20 Python
Python常见反爬虫机制解决方案
2020/06/01 Python
Python实现爬取并分析电商评论
2020/06/19 Python
python批量修改文件名的示例
2020/09/27 Python
python Pexpect模块的使用
2020/12/25 Python
html5本地存储_动力节点Java学院整理
2017/07/12 HTML / CSS
多视角3D可旋转的HTML5 Logo动画
2016/03/02 HTML / CSS
泰国健康和美容服务预订网站:GoWabi
2019/06/03 全球购物
马智宇结婚主持词
2014/04/01 职场文书
经济管理专业求职信
2014/06/09 职场文书
中学生秋季运动会广播稿
2014/09/21 职场文书
Mysql MVCC机制原理详解
2021/04/20 MySQL
golang 实现菜单树的生成方式
2021/04/28 Golang
详解TypeScript的基础类型
2022/02/18 Javascript
一文了解MYSQL三大范式和表约束
2022/04/03 MySQL