Python爬虫,带你制作高逼格的数据聚合云图

时间:2022-05-06
本文章向大家介绍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

首先:

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

令人尴尬的是,这个网站在绘制图的时候不支持中文,需要你从c:/windows/fonts下选择一个支持中文的字体,mac 用户从windows拷下文件夹也可以,或者在网上下。

然后点击Visulize就可以生成高逼格的云图了。讲解完毕,有什么需要改进的请大家留言。

源码下载:https://github.com/forezp/ZhihuSpiderMan/tree/master/blogspider