使用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 xml.etree.ElementTree遍历xml所有节点实例详解
Dec 04 Python
Python编程实现使用线性回归预测数据
Dec 07 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
解决pycharm py文件运行后停止按钮变成了灰色的问题
Nov 29 Python
python 自定义对象的打印方法
Jan 12 Python
详解Python3除法之真除法、截断除法和下取整对比
May 23 Python
Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法
Sep 24 Python
Python queue队列原理与应用案例分析
Sep 27 Python
python实现扫雷小游戏
Apr 24 Python
在pycharm中关掉ipython console/PyDev操作
Jun 09 Python
tensorflow图像裁剪进行数据增强操作
Jun 30 Python
Python requests用法和django后台处理详解
Mar 19 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生成便于打印的网页
2006/10/09 PHP
PHP语言中global和$GLOBALS[]的分析 之二
2012/02/02 PHP
zend framework框架中url大小写问题解决方法
2014/08/19 PHP
PHP strtotime函数用法、实现原理和源码分析
2015/02/04 PHP
jQuery 添加/移除CSS类实现代码
2010/02/11 Javascript
浅谈tudou土豆网首页图片延迟加载的效果
2010/06/23 Javascript
16个最流行的JavaScript框架[推荐]
2011/05/29 Javascript
理运用命名空间让js不产生冲突避免全局变量的泛滥
2014/06/15 Javascript
使用JavaScript和C#中获得referer
2014/11/14 Javascript
浅析javascript中函数声明和函数表达式的区别
2015/02/15 Javascript
基于JavaScript实现瀑布流效果(循环渐近)
2016/01/27 Javascript
Node.js+Express配置入门教程详解
2016/05/19 Javascript
vue增删改查的简单操作
2017/07/15 Javascript
BetterScroll 在移动端滚动场景的应用
2017/09/18 Javascript
jqgrid实现简单的单行编辑功能
2017/09/30 Javascript
JS实现非首屏图片延迟加载的示例
2018/01/06 Javascript
[01:32]完美世界DOTA2联赛10月29日精彩集锦
2020/10/30 DOTA
Python中的作用域规则详解
2015/01/30 Python
Python中的tuple元组详细介绍
2015/02/02 Python
Python Sqlite3以字典形式返回查询结果的实现方法
2016/10/03 Python
Python时间的精准正则匹配方法分析
2017/08/17 Python
pycharm下打开、执行并调试scrapy爬虫程序的方法
2017/11/29 Python
利用Python如何生成hash值示例详解
2017/12/20 Python
python中format()函数的简单使用教程
2018/03/14 Python
将字典转换为DataFrame并进行频次统计的方法
2018/04/08 Python
Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例
2018/06/09 Python
Python3中详解fabfile的编写
2018/06/24 Python
浅析python,PyCharm,Anaconda三者之间的关系
2019/11/27 Python
Visual Studio Code搭建django项目的方法步骤
2020/09/17 Python
Python定时任务框架APScheduler原理及常用代码
2020/10/05 Python
银行演讲稿范文
2014/01/03 职场文书
妇女儿童发展规划实施方案
2014/03/16 职场文书
云南省召开党的群众路线教育实践活动总结会议新闻稿
2014/10/21 职场文书
讲座通知范文
2015/04/23 职场文书
朋友聚会开场白
2015/06/01 职场文书
父亲去世追悼词
2015/06/23 职场文书