微博爬虫及舆情分析-3.文本清理与制作词云

1、文本清理

import pandas as pd
import pymysql
from sqlalchemy import create_engine
import re
import jieba
import jieba.analyse
#1.从数据库导入微博数据并查看
mblog_frame = pd.read_csv('mblog.csv',index_col=None)
mblog_frame.head(2)

在这里插入图片描述

# 2.清除text中的非微博正文字符并抽取关键词
# 自定义函数
def clean_text(raw):
    """
    清除text中的非微博正文字符
    返回值类型为元组
    """
    if raw['raw_text']:
        text=re.sub('<[^<]*>','',raw['raw_text']) # 清除多余的html语句
        text=re.sub('[#\n]*','',text) # 清除换行符与#符号
        text=re.sub('(http://.*)$','',text) # 清除文末的网址
        return text 
    else:
        return None
def get_chinese_text(raw):
    """
    清除text中的非中文字符
    只能提取中文字符,微博中的数字以及英文均会丢失
    """
    if raw['text']:
        res_text=''.join(re.findall(r"[\u4e00-\u9fff]{2,}",raw['text']))
        return (raw['mid'],res_text)
    else:
        return None

def get_keywords(raw):
    """
    使用jieba从中文text抽取关键词
    默认抽取20个关键词
    longtext 提取40个关键词
    """
    if raw['chinese_text']:
        if raw['isLongText'] == 1:
            # 当text为长文本时,提取50个关键词
            keywords = jieba.analyse.extract_tags(raw['chinese_text'],topK=50)
        else:
            # 当text为非长文本时,默认提取20个关键词
            keywords = jieba.analyse.extract_tags(raw['chinese_text'])
        return (raw['mid'],keywords)
    else:
        return None

def clean_created_date(raw):
    created_date = raw['created_at']
    if created_date.endswith('前'):
        created_date = '09-15'
    elif created_date.startswith('昨天'):
        created_date = '09-14'
    return created_date
#获取清理后的created_date
mblog_frame['created_date'] = mblog_frame.apply(clean_created_date,axis=1)
# 获取清理后的text
mblog_frame['chinese_text'] = mblog_frame.apply(clean_text,axis=1)

# 以传入字典items()的形式生成DataFrame,指定列名
res_mblog = pd.DataFrame(mblog_frame,columns=['mid','chinese_text','like_count','comments_count','reposts_count','created_date','user_id'])
# 写入csv文件便于查看数据清洗结果
res_mblog.to_csv('clean_mblog.csv', encoding='utf_8_sig',index=False)
# 获取关键字并转换为分散存储的DataFrame
mid_with_keyword = list(mblog_frame.apply(get_keywords,axis=1))
# 这里要把keywords列表存储到数据库,因此需要将keywords列表分开,并与mid对应
keywords_list = [(raw[0],w) for raw in mid_with_keyword for w in raw[1]]
mid_with_keyword = pd.DataFrame(keywords_list,columns=['mid','keyword'])
# 写入csv文件便于查看结果
mid_with_keyword.to_csv('keyword.csv', encoding='utf_8_sig',index=False)

2、制作词云

# 从数据库读取微博数据
keyword_frame = pd.read_csv('keyword.csv',index_col=False)
# 取出全部的关键词,并生成一个列表
all_keyword = list(keyword_frame.keyword)

# 使用collections模块中的Counter统计每个关键词出现的次数,Counter返回一个字典,keyword:count
from collections import Counter
word_freq_frame = pd.DataFrame(Counter(all_keyword).items())
word_freq_frame.columns=['word','count']
top100_freq_word = word_freq_frame.sort_values('count',ascending=0).head(100)
top100_freq_word_dict=dict(list(top100_freq_word.apply(lambda w:(w['word'],w['count']),axis=1)))

from wordcloud import WordCloud,STOPWORDS
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']#用来显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来显示负号
%matplotlib inline
plt.rcParams['figure.dpi'] = 100 #分辨率
wc = WordCloud(background_color="white",max_words=2000,font_path='simhei.ttf')
wc.generate_from_frequencies(top100_freq_word_dict)
plt.imshow(wc)
plt.axis('off')
plt.show()

在这里插入图片描述

  • 6
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值