zoukankan      html  css  js  c++  java
  • 爬虫大作业-爬取B站弹幕

    1.选一个自己感兴趣的主题或网站。(所有同学不能雷同)

    https://www.bilibili.com/video/av22224421

    2.用python 编写爬虫程序,从网络上爬取相关主题的数据。

    3.对爬了的数据进行文本分析,生成词云。

    import  requests
    import jieba
    import pandas
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud ,ImageColorGenerator
    from bs4 import BeautifulSoup
    
    def jieba_cut(sentence):
        seg = jieba.cut(sentence)
        segList = []
        for i in seg:
            segList.append((i))
        return segList
    
    
    
    if __name__=='__main__':
         str=''
         url='http://comment.bilibili.com/36773399.xml'
         page=requests.get(url)
         page.encoding='utf-8'
         soup=BeautifulSoup(page.text,"html.parser")
         content=soup.find_all('d')
         for i in content:
            str=str+i.text
         with open('bilibili.txt','w',encoding='utf-8') as f:
            f.write(str)
             
         dict={}
        
         with open ('bilibili.txt','r',encoding='utf-8') as f:
            words=jieba_cut(f.read())
            wordslist=set(words)
            for word in wordslist:
                dict[word]=words.count(word)
    
            mask = plt.imread(r'H:129wallhaven-627476.jpg')
    
    
            text=' '.join(words)
            wc = WordCloud(
                width=1000,
                height=800,
                margin=2,
                background_color='white',  # 设置背景颜色
                font_path='C:WindowsFontsSTZHONGS.TTF',  # 若是有中文的话,这句代码必须添加,不然会出现方框,不出现汉字
                max_words=1000,  # 设置最大现实的字数
                max_font_size=400,  # 设置字体最大值
                random_state=50,  # 设置有多少种随机生成状态,即有多少种配色方案
                mask=mask,
            )
            mycloud = wc.generate(text)
            image_colors = ImageColorGenerator(mask)
    
            wc.recolor(color_func=image_colors)
            wc.to_file('cloudword.jpg')

    4.对文本分析结果进行解释说明。

    5.写一篇完整的博客,描述上述实现过程、遇到的问题及解决办法、数据分析思想及结论。

     找到视频网站,查找网页源码,找出cid,打开弹幕文件XML,开始爬取弹幕存入文本中。在词频统计时出现一点小问题,用字典统计。

    6.最后提交爬取的全部数据、爬虫及数据分析源代码。

     

     
  • 相关阅读:
    错误 com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value
    mysql数据库
    c程序语言设计
    第二十四天 注解 和 异常
    第二十三天 泛型
    第二十二天 集合
    php tostring用法
    thinkphp批量添加水印
    thinkphp fetchSql
    php钩子是什么意思
  • 原文地址:https://www.cnblogs.com/129lai/p/8921280.html
Copyright © 2011-2022 走看看