爬取哔哩哔哩弹幕制作词云

爬取哔哩哔哩的弹幕,http://comment.bilibili.com/6315651.xml

需要知道cid,可以F12,F5刷新,找cid,找到之后拼接url

也可以写代码,解析response获取cid,然后再拼接

使用requests或者urllib都可以

我是用requests,请求该链接获取到xml文件

代码:获取xml

def get_data():
    res = requests.get('http://comment.bilibili.com/6315651.xml')
    res.encoding = 'utf8'
    with open('gugongdanmu.xml', 'a', encoding='utf8') as f:
        f.writelines(res.text)

解析xml,

def analyze_xml():
    f1 = open("gugongdanmu.xml", "r", encoding='utf8')
    f2 = open("tanmu2.txt", "w", encoding='utf8')
    count = 0
    # 正则匹配解决xml的多余的字符
    dr = re.compile(r'<[^>]+>', re.S)
    while 1:
        line = f1.readline()
        if not line:
            break
        pass
        # 匹配到之后用空代替
        dd = dr.sub('', line)
        # dd = re.findall(dr, line)
        count = count+1
        f2.writelines(dd)
    print(count)

去掉无用的字符和数字,找出所有的汉字

def analyze_hanzi():

    f1 = open("tanmu2.txt", "r", encoding='utf8')
    f2 = open("tanmu3.txt", "w",  encoding='utf8')
    count = 0
    # dr = re.compile(r'<[^>]+>',re.S)
    # 所有的汉字[一-龥]
    dr = re.compile(r'[一-龥]+',re.S)
    while 1:
        line = f1.readline()
        if not line:
            break
        pass
        # 找出无用的符号和数字
        # dd = dr.sub('',line)
        dd = re.findall(dr, line)
        count = count+1
        f2.writelines(dd)
    print(count)
    # pattern = re.compile(r'[一-龥]+')

使用jieba分词,生成词云

def show_sign():

    content = read_txt_file()
    segment = jieba.lcut(content)
    words_df = pd.DataFrame({'segment': segment})

    stopwords = pd.read_csv("stopwords.txt", index_col=False, quoting=3, sep=" ", names=['stopword'], encoding='utf-8')
    words_df = words_df[~words_df.segment.isin(stopwords.stopword)]
    print(words_df)
    print('-------------------------------')
    words_stat = words_df.groupby(by=['segment'])['segment'].agg(numpy.size)
    words_stat = words_stat.to_frame()
    words_stat.columns = ['计数']
    words_stat = words_stat.reset_index().sort_values(by=["计数"], ascending=False)

    # 设置词云属性
    color_mask = imread('ciyun.png')
    wordcloud = WordCloud(font_path="simhei.ttf",  # 设置字体可以显示中文
                          background_color="white",  # 背景颜色
                          max_words=1000,  # 词云显示的最大词数
                          mask=color_mask,  # 设置背景图片
                          max_font_size=100,  # 字体最大值
                          random_state=42,
                          width=1000, height=860, margin=2,
                          # 设置图片默认的大小,但是如果使用背景图片的话,                                                   # 那么保存的图片大小将会按照其大小保存,margin为词语边缘距离
                          )

    # 生成词云, 可以用generate输入全部文本,也可以我们计算好词频后使用generate_from_frequencies函数
    word_frequence = {x[0]: x[1] for x in words_stat.head(1000).values}
    print(word_frequence)
    # for key,value in word_frequence:
    #     write_txt_file(word_frequence)
    word_frequence_dict = {}
    for key in word_frequence:
        word_frequence_dict[key] = word_frequence[key]

    wordcloud.generate_from_frequencies(word_frequence_dict)
    # 从背景图片生成颜色值
    image_colors = ImageColorGenerator(color_mask)
    # 重新上色
    wordcloud.recolor(color_func=image_colors)
    # 保存图片
    wordcloud.to_file('output.png')
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()

运行程序,结果:

统计的结果

完成!

pip的换源,原来的太慢,然后将你自己没有库装上

猜你喜欢

转载自blog.csdn.net/zhao_5352269/article/details/83273153