使用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 相关文章推荐
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
Jun 18 Python
在Python的Flask框架中实现单元测试的教程
Apr 20 Python
python获得文件创建时间和修改时间的方法
Jun 30 Python
python pandas 组内排序、单组排序、标号的实例
Apr 12 Python
Python处理CSV与List的转换方法
Apr 19 Python
Python实现的调用C语言函数功能简单实例
Mar 13 Python
Python Process多进程实现过程
Oct 22 Python
Python Socketserver实现FTP文件上传下载代码实例
Mar 27 Python
Jupyter打开图形界面并画出正弦函数图像实例
Apr 24 Python
Python基于smtplib协议实现发送邮件
Jun 03 Python
python 多线程中join()的作用
Oct 29 Python
Python中过滤字符串列表的方法
Dec 22 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+Ajax+JS实现多图上传
2016/05/07 PHP
Laravel 默认邮箱登录改成用户名登录的实现方法
2019/08/12 PHP
laravel5.5添加echarts实现画图功能的方法
2019/10/09 PHP
跟随鼠标旋转的文字
2006/11/30 Javascript
使用闭包对setTimeout进行简单封装避免出错
2013/07/10 Javascript
JqueryMobile动态生成listView并实现刷新的两种方法
2014/03/05 Javascript
使用jquery组件qrcode生成二维码及应用指南
2015/02/22 Javascript
node.js操作mongodb学习小结
2015/04/25 Javascript
深入浅析JavaScript中的arguments对象(强力推荐)
2016/06/03 Javascript
javascript创建含数字字母的随机字符串方法总结
2016/08/01 Javascript
JavaScript 对引擎、运行时、调用堆栈的概述理解
2018/10/22 Javascript
vue实现将一个数组内的相同数据进行合并
2019/11/07 Javascript
jQuery实现颜色打字机的完整代码
2020/03/19 jQuery
[42:25]EG vs Spirit Supermajor 败者组 BO3 第二场 6.4
2018/06/05 DOTA
[04:22]DOTA2大事件之护国神翼
2020/08/14 DOTA
python中django框架通过正则搜索页面上email地址的方法
2015/03/21 Python
python实现的AES双向对称加密解密与用法分析
2017/05/02 Python
python pyinstaller 加载ui路径方法
2019/06/10 Python
Python发展史及网络爬虫
2019/06/19 Python
django url到views参数传递的实例
2019/07/19 Python
Anaconda详细安装步骤图文教程
2020/11/12 Python
详解Pycharm第三方库的安装及使用方法
2020/12/29 Python
canvas实现图片镜像翻转的2种方式
2020/07/22 HTML / CSS
美国购车网站:TrueCar
2016/10/19 全球购物
strlen的几种不同实现方法
2013/05/31 面试题
成人继续教育实施方案
2014/03/01 职场文书
网络编辑职责
2014/03/01 职场文书
初中学生评语大全
2014/04/24 职场文书
大学学生会竞选演讲稿
2014/04/25 职场文书
销售口号大全
2014/06/11 职场文书
寻找最美家庭活动方案
2014/08/20 职场文书
高中课前三分钟演讲稿
2014/09/13 职场文书
群众路线批评与自我批评发言稿
2014/10/16 职场文书
先进党员事迹材料
2014/12/24 职场文书
委托书的样本
2015/01/28 职场文书
Vue ECharts实现机舱座位选择展示功能
2022/05/15 Vue.js