使用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制作游戏外挂
Jan 04 Python
Python模块文件结构代码详解
Feb 03 Python
PyCharm安装第三方库如Requests的图文教程
May 18 Python
python实现三维拟合的方法
Dec 29 Python
Python数据类型之Set集合实例详解
May 07 Python
Python实现操纵控制windows注册表的方法分析
May 24 Python
解决django中ModelForm多表单组合的问题
Jul 18 Python
pandas DataFrame 警告(SettingWithCopyWarning)的解决
Jul 23 Python
django 基于中间件实现限制ip频繁访问过程详解
Jul 30 Python
Python使用mongodb保存爬取豆瓣电影的数据过程解析
Aug 14 Python
python解释器spython使用及原理解析
Aug 24 Python
MAC平台基于Python Appium环境搭建过程图解
Aug 13 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
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
php绘制一条弧线的方法
2015/01/24 PHP
了解PHP的返回引用和局部静态变量
2015/06/04 PHP
php创建桌面快捷方式实现方法
2015/12/31 PHP
PHP实现登陆并抓取微信列表中最新一组微信消息的方法
2017/07/10 PHP
浅谈PHP发送HTTP请求的几种方式
2017/07/25 PHP
jquery动画4.升级版遮罩效果的图片走廊--带自动运行效果
2012/08/24 Javascript
DIV+CSS+JS不间断横向滚动实现代码
2013/03/19 Javascript
js弹窗返回值详解(window.open方式)
2014/01/11 Javascript
Jquery 过滤器(first,last,not,even,odd)的使用
2014/01/22 Javascript
jQuery 如何先创建、再修改、后添加DOM元素
2014/05/20 Javascript
深入解读JavaScript中的Hoisting机制
2015/08/12 Javascript
javascript实现图片上传前台页面
2015/08/18 Javascript
使用jQuery调用XML实现无刷新即时聊天
2016/08/07 Javascript
BOM系列第一篇之定时器setTimeout和setInterval
2016/08/17 Javascript
JS之相等操作符详解
2016/09/13 Javascript
xcode中获取js文件的路径方法(推荐)
2016/11/05 Javascript
Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程
2017/05/07 Javascript
原生JavaScript实现todolist功能
2018/03/02 Javascript
VSCode中如何利用d.ts文件进行js智能提示
2018/04/13 Javascript
优雅的在React项目中使用Redux的方法
2018/11/10 Javascript
Vue实现input宽度随文字长度自适应操作
2020/07/29 Javascript
自己编程中遇到的Python错误和解决方法汇总整理
2015/06/03 Python
Python 12306抢火车票脚本
2018/02/07 Python
Python根据已知邻接矩阵绘制无向图操作示例
2018/06/23 Python
pandas DataFrame的修改方法(值、列、索引)
2019/08/02 Python
python线程安全及多进程多线程实现方法详解
2019/09/27 Python
python代码区分大小写吗
2020/06/17 Python
为什么是 Python -m
2020/06/19 Python
基于 HTML5 Canvas实现 的交互式地铁线路图
2018/03/05 HTML / CSS
瑜伽服装品牌:露露柠檬(lululemon athletica)
2017/06/04 全球购物
英国领先的在线高尔夫设备零售商:Golfgeardirect
2020/12/11 全球购物
交通事故协议书范文
2014/10/23 职场文书
2014年财务科工作总结
2014/11/11 职场文书
2014公司年终工作总结
2014/12/19 职场文书
php 原生分页
2021/04/01 PHP