周末了,八一八推荐博客的排名

作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢!

最近挖煤君上了博客园推荐博客的排行榜,甚是高兴。看着自己名次上升,是件很开心的事情。

看着推荐榜上的诸位大神,再加上QQ群里的交流,我骨子里的八卦精神又一次发痒,所以就做了个爬虫,把推荐榜上各位的入园时间、粉丝数、排名给搜罗了一下,做成一个泡泡图。

看起来,排名不是完全由粉丝数决定的,但也有相当大的相关性。大伙的眼睛是雪亮的啊。

看到肥嘟嘟沉淀在下面的诸位大神,挖煤君表示由衷敬佩。

大家来找自己的泡泡吧。Vamei躲在右下角的小角落哦!

挖煤君的小小爬虫是Python写的,图是D3.js画的。欢迎点赞留言加粉儿哦,挖煤君也想向下沉淀沉淀。

 

泡泡的面积和粉丝数成正比,x轴为入园时间,y轴为排名。

D3老的浏览器可能不支持。Chrome效果最佳。各位能给我反馈一下不同浏览器的效果如何?

2014.03.08数据更新

Python爬虫代码:

#-*- coding: UTF-8 -*-
# By Vamei
# scrape the cnblogs

import requests
import BeautifulSoup

import re
import json
from datetime import datetime

def read_page(url, method="get"):
    '''
    read the html page via the URL
    '''
    status_code = 0
    while status_code != 200:
        if method == "get":
            r = requests.get(url)
        elif method == "post":
            r = requests.post(url)
        status_code = r.status_code
        print status_code
    page = r.content
    return page

def parse_person_profile(relative, info={}):
    '''
    retrieve the information from the personal profile page
    '''

    r = read_page("http://home.cnblogs.com/u%s" % relative)
    soup  = BeautifulSoup.BeautifulSoup(r)

    # the count of the followers
    el            = soup.find("a", {'id':"follower_count"})
    info['粉丝数']   =  int(el.getText())

    # the time of the registration
    el       = soup.find("div", {'id': "ctl00_cphMain_panel_profile"})
    profile  =  el.ul
    reg_time =  el.ul.findChildren()[0]
    raw = reg_time.getText()
    m   = re.findall("(d+)", raw)
    m   =  map(int, m)
    dt  = datetime(year=m[0], month=m[1], day=m[2])
    info['开博时间'] = dt.strftime("%Y%m%d")
    return info

def cnblogs_recommend_150():
    '''
    workhouse
    '''
    url = "http://www.cnblogs.com/aggsite/ExpertBlogs"
    r = read_page(url, method="post")
    soup = BeautifulSoup.BeautifulSoup(r)

    # retrieve the information blogger by blogger
    info = []
    anchors = soup.findAll('a')
    # blogger by blogger
    for i, a in enumerate(anchors):
        name = a.getText()
        p_info = {'昵称': name, '排名': i + 1}
        # parse_person_main(a['href'], p_info)
        parse_person_profile(a['href'], p_info)

        info.append(p_info)

    # write the retrieved data into the file
    with open("info", "w") as f:
        rlt = json.dumps(info, indent=4, encoding="UTF-8", ensure_ascii=False)
        f.write(rlt.encode("utf8"))
    return info

if __name__ == "__main__":
    info = cnblogs_recommend_150()

Javascript代码所在位置

原文地址:https://www.cnblogs.com/vamei/p/3583398.html