专栏名称: 程序猿
本微信公众号:imkuqin,为程序员提供最新最全的编程学习资料的查询。目前已经开通PHP、C/C++函数库、.NET Framework类库、J2SE API查询功能。
目录
相关文章推荐
程序员小灰  ·  疯了!下载 DeepSeek 最高判20年! ·  2 天前  
程序猿  ·  450万虚假Star曝光,最低0.7元/个? ... ·  昨天  
程序员的那些事  ·  美国人下载 DeepSeek,最高判 20 ... ·  3 天前  
OSC开源社区  ·  萨姆·奥特曼:OpenAI在开源问题上一直处 ... ·  5 天前  
51好读  ›  专栏  ›  程序猿

Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图

程序猿  · 公众号  · 程序员  · 2017-06-08 21:46

正文

本文出自方志朋的博客

链接:http://blog.csdn.net/forezp/article/details/70198541 (点击尾部阅读原文前往)


一时兴起,想用Python爬爬自己的博客,通过数据聚合,制作高逼格的云图(对词汇出现频率视觉上的展示),看看最近我到底写了啥文章。


一、直接上几张我的博客数据的云图


1.1 爬取文章的标题的聚合



1.2 爬取文章的摘要的聚合



1.3 爬取文章的标题+摘要的聚合



我最近写了SpringCloud系列教程,还有一些微服务架构方面,从云图上看,基本吻合。你若不信,可以进我的博客看看,数据还是非常准确的


二、技术栈


  • 开发工具: pycharm

  • 爬虫技术:bs64、requsts、jieba

  • 分析工具:wordArt


三、爬虫构架设计



整个爬虫架构非常简单:


  • 爬取我的博客:http://blog.csdn.net/forezp

  • 获取数据

  • 将数据用“结巴”库,分词。

  • 将得到的数据在在artword上制作云图。

  • 将制作出来的云图展示给用户。


四、具体实现


先根据博客地址爬去数据:

url = 'http://blog.csdn.net/forezp'   titles=set()   

def download(url):   
    if url is None:   
        return None   
    try:   
        response = requests.get(url, headers={   
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',   
        })   
        if (response.status_code == 200):   
            return response.content   
        return None   
    except:   
        return None

解析标题

def parse_title(html):   
    if html is None:   
        return None   
    soup = BeautifulSoup(html, "html.parser")   
    links = soup.find_all('a', href=re.compile(r'/forezp/article/details'))   
    for link in links:   

        titles.add(link.get_text())

解析摘要:

def parse_descrtion(html):   
    if html is None:   
        return None   
    soup=BeautifulSoup(html, "html.parser")   
    disciptions=soup.find_all('div',attrs={'class': 'article_description'})   
    for link in disciptions:   

        titles.add(link.get_text())

用“结巴”分词,”激8”分词怎么用,看这里: https://github.com/fxsjy/jieba/

def jiebaSet():   
    strs=''   
    if titles.__len__()==0:   
        return   
    for item in titles:   
        strs=strs+item;   

    tags = jieba.analyse.extract_tags(strs, topK=100, withWeight=True)   
    for item in tags:   
        print(item[0] + '\t' + str(int(item[1] * 1000)))

因为数据比较少,所以我直接打印在控制台,并把它复制下来,更好的方法是存在MongoDB中。


制作云图:

用 artword在线工具,地址:https://wordart.com


首先:


导入从控制台复制过来的数据:








请到「今天看啥」查看全文