使用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 相关文章推荐
用PyQt进行Python图形界面的程序的开发的入门指引
Apr 14 Python
Python实现简单多线程任务队列
Feb 27 Python
python实现爬取图书封面
Jul 05 Python
Python分支语句与循环语句应用实例分析
May 07 Python
pandas实现to_sql将DataFrame保存到数据库中
Jul 03 Python
树莓派3 搭建 django 服务器的实例
Aug 29 Python
Python字符串中添加、插入特定字符的方法
Sep 10 Python
Python 类,property属性(简化属性的操作),@property,property()用法示例
Oct 12 Python
python安装gdal的两种方法
Oct 29 Python
python中的数组赋值与拷贝的区别详解
Nov 26 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
May 11 Python
深度学习详解之初试机器学习
Apr 14 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分页代码学习示例分享
2014/02/20 PHP
纯PHP生成的一个树叶图片画图例子
2014/04/16 PHP
js GridView 实现自动计算操作代码
2009/03/25 Javascript
基于jquery实现拆分姓名的方法(纯JS版)
2013/05/08 Javascript
Jquery操作js数组及对象示例代码
2014/05/11 Javascript
node.js中的fs.fchmod方法使用说明
2014/12/16 Javascript
Javascript与jQuery方法的隐藏与显示
2015/01/19 Javascript
JavaScript的原型继承详解
2015/02/15 Javascript
javascript变量声明实例分析
2015/04/25 Javascript
跟我学习javascript的最新标准ES6
2015/11/20 Javascript
jQuery解析Json实例详解
2015/11/24 Javascript
javascript实现无缝上下滚动特效
2015/12/16 Javascript
模拟javascript中的sort排序(简单实例)
2016/08/17 Javascript
纯JS实现可拖拽表单的简单实例
2016/09/02 Javascript
jquery Ajax 全局调用封装实例详解
2017/01/16 Javascript
jQuery插件zTree实现删除树子节点的方法示例
2017/03/08 Javascript
Vue使用zTree插件封装树组件操作示例
2019/04/25 Javascript
微信小程序的授权实现过程解析
2019/08/02 Javascript
JavaScript进制转换实现方法解析
2020/01/18 Javascript
使用Node.js实现base64和png文件相互转换的方法
2020/03/11 Javascript
基于JS实现视频上传显示进度条
2020/05/12 Javascript
JavaScript实现筛选数组
2021/03/02 Javascript
Python实现115网盘自动下载的方法
2014/09/30 Python
Python格式化css文件的方法
2015/03/10 Python
谈谈如何手动释放Python的内存
2016/12/17 Python
Python随机生成均匀分布在三角形内或者任意多边形内的点
2017/12/14 Python
Python3 元组tuple入门基础
2020/02/09 Python
Python控制鼠标键盘代码实例
2020/12/08 Python
联想中国官方商城:Lenovo China
2017/10/18 全球购物
怎样在 Applet 中建立自己的菜单(MenuBar/Menu)?
2012/06/20 面试题
小学生元旦感言
2014/02/26 职场文书
银行贷款承诺书
2014/03/29 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
单位租房协议书样本
2014/10/30 职场文书
外贸英文求职信范文
2015/03/19 职场文书
2015上半年个人工作总结
2015/07/27 职场文书