使用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解析JSON数据的基本方法
Oct 15 Python
python使用fork实现守护进程的方法
Nov 16 Python
django模板语法学习之include示例详解
Dec 17 Python
pandas DataFrame 行列索引及值的获取的方法
Jul 02 Python
基于python操作ES实例详解
Nov 16 Python
Python+OpenCV 实现图片无损旋转90°且无黑边
Dec 12 Python
django多种支付、并发订单处理实例代码
Dec 13 Python
python 实现分组求和与分组累加求和代码
May 18 Python
新手学python应该下哪个版本
Jun 11 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
Dec 07 Python
python调用jenkinsAPI构建jenkins,并传递参数的示例
Dec 09 Python
python 模拟登录B站的示例代码
Dec 15 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
espresso double下 咖啡粉超细时 饼压力对咖啡的影响
2021/03/03 冲泡冲煮
php将数据库中所有内容生成静态html文档的代码
2010/04/12 PHP
php设计模式 Template (模板模式)
2011/06/26 PHP
PHP中操作ini配置文件的方法
2013/04/25 PHP
CodeIgniter实现从网站抓取图片并自动下载到文件夹里的方法
2015/06/17 PHP
JavaScript数组方法总结分析
2016/05/06 Javascript
AngularJS入门教程之过滤器详解
2016/08/19 Javascript
微信小程序 页面跳转及数据传递详解
2017/03/14 Javascript
jQuery插件FusionCharts绘制2D柱状图和折线图的组合图效果示例【附demo源码】
2017/04/10 jQuery
nodeJS实现简单网页爬虫功能的实例(分享)
2017/06/08 NodeJs
AngularJS常见过滤器用法实例总结
2017/07/06 Javascript
js实现鼠标拖拽多选功能示例
2017/08/01 Javascript
PHP 实现一种多文件上传的方法
2017/09/20 Javascript
浅谈JavaScript的innerWidth与innerHeight
2017/10/12 Javascript
vue2.0在没有dev-server.js下的本地数据配置方法
2018/02/23 Javascript
JavaScript类数组对象转换为数组对象的方法实例分析
2018/07/24 Javascript
Angular6笔记之封装http的示例代码
2018/07/27 Javascript
详解nodejs解压版安装和配置(带有搭建前端项目脚手架)
2018/12/06 NodeJs
JS+php后台实现文件上传功能详解
2019/03/02 Javascript
JavaScript静态作用域和动态作用域实例详解
2019/06/17 Javascript
详解Python中的正则表达式的用法
2015/04/09 Python
Python 模块EasyGui详细介绍
2017/02/19 Python
python装饰器实例大详解
2017/10/25 Python
Python使用requests发送POST请求实例代码
2018/01/25 Python
浅谈Pandas 排序之后索引的问题
2018/06/07 Python
Python 函数返回值的示例代码
2019/03/11 Python
scrapy-redis源码分析之发送POST请求详解
2019/05/15 Python
python文件操作的简单方法总结
2019/11/07 Python
英国领先的运动营养品牌:Protein Dynamix
2018/01/02 全球购物
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
在C语言中实现抽象数据类型什么方法最好
2014/06/26 面试题
拾金不昧表扬稿
2015/01/16 职场文书
国王的演讲观后感
2015/06/03 职场文书
小学生运动会广播
2015/08/19 职场文书
评测 | 大屏显示带收音机的高端音箱,JBL TUNE2便携式插卡音箱实测
2021/04/24 无线电
基于Python实现股票收益率分析
2022/04/02 Python