1. 程式人生 > 實用技巧 >Python爬蟲,帶你製作高逼格的資料聚合雲圖

Python爬蟲,帶你製作高逼格的資料聚合雲圖

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理

以下文章來源於搜狗網 作者:python漲薪研究所

( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料。 )

一時興起,想用Python爬爬自己的部落格,通過資料聚合,製作高逼格的雲圖(對詞彙出現頻率視覺上的展示),看看最近我到底寫了啥文章。

一、直接上幾張我的部落格資料的雲圖

1.1 爬取文章的標題的聚合


1.2 爬取文章的摘要的聚合

1.3 爬取文章的標題+摘要的聚合

我最近寫了SpringCloud系列教程,還有一些微服務架構方面,從雲圖上看,基本吻合。你若不信,可以進我的部落格看看,資料還是非常準確的

二、技術棧

開發工具: pycharm

爬蟲技術:bs64、requsts、jieba

分析工具:wordArt

三、爬蟲構架設計

整個爬蟲架構非常簡單:

爬取我的部落格:https://mp.csdn.net/console/article

獲取資料

將資料用“結巴”庫,分詞。

將得到的資料在在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就可以生成高逼格的雲圖了。講解完畢,有什麼需要改進的請大家留言。