使用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检测一个对象是否为字符串类的方法
May 21 Python
python笔记:mysql、redis操作方法
Jun 28 Python
tensorflow输出权重值和偏差的方法
Feb 10 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
Jul 11 Python
Python 获取主机ip与hostname的方法
Dec 17 Python
基于Python实现迪杰斯特拉和弗洛伊德算法
May 27 Python
详解重置Django migration的常见方式
Feb 15 Python
Python缓存技术实现过程详解
Sep 25 Python
pandas中的数据去重处理的实现方法
Feb 10 Python
jupyter lab的目录调整及设置默认浏览器为chrome的方法
Apr 10 Python
Python hashlib和hmac模块使用方法解析
Dec 08 Python
Python几种酷炫的进度条的方式
Apr 11 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
DOTA2【瓜皮时刻】Vol.91 RTZ山史最惨“矿难”
2021/03/05 DOTA
PHP4中实现动态代理
2006/10/09 PHP
风格模板初级不完全修改教程
2006/10/09 PHP
php获取客户端电脑屏幕参数的方法
2015/01/09 PHP
PHP会话处理的10个函数
2015/08/11 PHP
PHP Laravel 上传图片、文件等类封装
2017/08/16 PHP
PHP后期静态绑定实例浅析
2018/12/21 PHP
基于laravel Request的所有方法详解
2019/09/29 PHP
jQuery1.6 使用方法一
2011/11/23 Javascript
javaScript面向对象继承方法经典实现
2013/08/20 Javascript
自编jQuery插件实现模拟alert和confirm
2014/09/01 Javascript
javascript排序函数实现数字排序
2015/06/26 Javascript
JavaScript中定义类的方式详解
2016/01/07 Javascript
javascript闭包功能与用法实例分析
2017/04/06 Javascript
VUE中使用MUI方法
2019/02/12 Javascript
小程序云开发获取不到数据库记录的解决方法
2019/05/18 Javascript
vue组件内部引入外部js文件的方法
2020/01/18 Javascript
[56:45]DOTA2上海特级锦标赛D组小组赛#1 EG VS COL第一局
2016/02/28 DOTA
[04:44]DOTA2西游记战队视频彩蛋流出 师徒开黑巧遇林书豪
2016/08/03 DOTA
Python中使用PyHook监听鼠标和键盘事件实例
2014/07/18 Python
Python 基础教程之str和repr的详解
2017/08/20 Python
pandas的qcut()方法详解
2019/07/06 Python
python PyQt5/Pyside2 按钮右击菜单实例代码
2019/08/17 Python
Python爬取知乎图片代码实现解析
2019/09/17 Python
python不到50行代码完成了多张excel合并的实现示例
2020/05/28 Python
Python 程序员必须掌握的日志记录
2020/08/17 Python
Javascript 高级手势使用介绍
2013/04/21 HTML / CSS
美国全球旅游运营商:Pacific Holidays
2018/06/18 全球购物
Stio官网:男女、儿童户外服装
2019/12/13 全球购物
视图的作用
2014/12/19 面试题
关于毕业的广播稿
2014/01/10 职场文书
干部行政关系介绍信
2014/01/17 职场文书
20年同学聚会邀请函
2014/02/04 职场文书
关于晚自习早退的检讨书
2014/09/13 职场文书
送达通知书
2015/04/25 职场文书
MySQL查询日期时间
2022/05/15 MySQL