使用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创建线程示例
May 06 Python
Python数据类型详解(二)列表
May 08 Python
python网络编程调用recv函数完整接收数据的三种方法
Mar 31 Python
用Python实现随机森林算法的示例
Aug 24 Python
Python中单、双下划线的区别总结
Dec 01 Python
python3获取当前文件的上一级目录实例
Apr 26 Python
Selenium控制浏览器常见操作示例
Aug 13 Python
python使用celery实现异步任务执行的例子
Aug 28 Python
python可视化实现KNN算法
Oct 16 Python
PyCharm 光标变成黑块的解决方式
Feb 06 Python
使用python实现学生信息管理系统
Feb 25 Python
Python竟然能剪辑视频
May 25 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安装为Apache DSO
2006/10/09 PHP
php递归创建和删除文件夹的代码小结
2012/04/13 PHP
php正则表达式基本知识与应用详解【经典教程】
2017/04/17 PHP
jquery实现图片左右间隔滚动特效(可自动播放)
2013/05/08 Javascript
angular.foreach 循环方法使用指南
2015/01/06 Javascript
JavaScript中的Math.LN2属性用法详解
2015/06/12 Javascript
Javascript实现鼠标右键特色菜单
2015/08/04 Javascript
基于Vuex无法观察到值变化的解决方法
2018/03/01 Javascript
layui点击左侧导航栏,实现不刷新整个页面,只刷新局部的方法
2019/09/25 Javascript
使用vue-cli3+typescript的项目模板创建工程的教程
2020/02/28 Javascript
Vue+Element ui 根据后台返回数据设置动态表头操作
2020/09/21 Javascript
解决vuex刷新数据消失问题
2020/11/12 Javascript
[38:21]2014 DOTA2国际邀请赛中国区预选赛5.21 TongFu VS LGD-CDEC
2014/05/22 DOTA
[55:32]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第二场
2018/04/05 DOTA
Python加pyGame实现的简单拼图游戏实例
2015/05/15 Python
python+pyqt实现右下角弹出框
2017/10/26 Python
python 从文件夹抽取图片另存的方法
2018/12/04 Python
Python 多维List创建的问题小结
2019/01/18 Python
python requests库爬取豆瓣电视剧数据并保存到本地详解
2019/08/10 Python
python实现ip地址查询经纬度定位详解
2019/08/30 Python
python自动发微信监控报警
2019/09/06 Python
pytorch实现保证每次运行使用的随机数都相同
2020/02/20 Python
Pytest框架之fixture的详细使用教程
2020/04/07 Python
美国猫狗药物和用品网站:PetCareRx
2017/01/05 全球购物
台湾线上百货零售购物平台:friDay购物
2017/08/18 全球购物
后勤园长自我鉴定
2013/10/17 职场文书
环保宣传标语
2014/06/12 职场文书
“九一八事变纪念日”国旗下讲话稿
2014/09/14 职场文书
2014年移动公司工作总结
2014/12/08 职场文书
小学生优秀评语
2014/12/29 职场文书
复兴之路观后感
2015/06/02 职场文书
天那边观后感
2015/06/09 职场文书
师范生教育见习总结
2015/06/23 职场文书
导游词之秦皇岛燕塞湖
2020/01/03 职场文书
我的收音机情缘
2022/04/05 无线电
浅谈Redis变慢的原因及排查方法
2022/06/21 Redis