使用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生成器实现微线程编程的教程
Apr 13 Python
Python PyQt4实现QQ抽屉效果
Apr 20 Python
python实现简单登陆流程的方法
Apr 22 Python
python实现合并多个list及合并多个django QuerySet的方法示例
Jun 11 Python
Python中的支持向量机SVM的使用(附实例代码)
Jun 26 Python
python全栈要学什么 python全栈学习路线
Jun 28 Python
python进程的状态、创建及使用方法详解
Dec 06 Python
python自动点赞功能的实现思路
Feb 26 Python
基于Python的OCR实现示例
Apr 03 Python
python dict乱码如何解决
Jun 07 Python
Python3 ffmpeg视频转换工具使用方法解析
Aug 10 Python
Python torch.flatten()函数案例详解
Aug 30 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
改造一台复古桌面收音机
2021/03/02 无线电
php Http_Template_IT类库进行模板替换
2009/03/19 PHP
php目录遍历函数opendir用法实例
2014/11/20 PHP
laravel中Redis队列监听中断的分析
2020/09/14 PHP
用htc组件制作windows选项卡
2007/01/13 Javascript
jquery 关键字“拖曳搜索”之“拖曳”以及 图片“提示自适应放大”效果 的实现
2010/04/18 Javascript
EXTJS记事本 当CompositeField遇上RowEditor
2011/07/31 Javascript
Javascript数组的排序 sort()方法和reverse()方法
2012/06/04 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
JavaScript转换与解析JSON方法实例详解
2015/11/24 Javascript
jQuery EasyUI Pagination实现分页的常用方法
2016/05/21 Javascript
Node.js Sequelize如何实现数据库的读写分离
2016/10/23 Javascript
微信小程序 页面跳转传递值几种方法详解
2017/01/12 Javascript
Javascript面试经典套路reduce函数查重
2017/03/23 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
Grunt针对静态文件的压缩,版本控制打包的实例讲解
2017/09/29 Javascript
js瀑布流布局的实现
2020/06/28 Javascript
Openlayers显示瓦片网格信息的方法
2020/09/28 Javascript
[51:44]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第二场
2018/04/04 DOTA
python快速查找算法应用实例
2014/09/26 Python
新手常见6种的python报错及解决方法
2018/03/09 Python
Centos7 Python3下安装scrapy的详细步骤
2018/03/15 Python
django利用request id便于定位及给日志加上request_id
2018/08/26 Python
django 将model转换为字典的方法示例
2018/10/16 Python
python3.x实现base64加密和解密
2019/03/28 Python
Python 脚本拉取 Docker 镜像问题
2019/11/10 Python
python生成13位或16位时间戳以及反向解析时间戳的实例
2020/03/03 Python
Jupyter Notebook远程登录及密码设置操作
2020/04/10 Python
Python列表嵌套常见坑点及解决方案
2020/09/30 Python
Pycharm编辑器功能之代码折叠效果的实现代码
2020/10/15 Python
使用CSS3美化HTML表单的技巧演示
2016/05/17 HTML / CSS
公司财务工作总结的自我评价
2013/11/23 职场文书
我们的节日元宵节活动总结
2015/02/06 职场文书
2019银行竞聘书
2019/06/21 职场文书
Java对文件的读写操作方法
2022/04/29 Java/Android
Docker容器harbor私有仓库部署和管理
2022/08/05 Servers