zoukankan      html  css  js  c++  java
  • b站弹幕的爬取以及词云的简单使用

    一.B站弹幕的爬取

      1.分析发现,其弹幕都是通过list.so?=cid这个文件加载出来的,所以我们找到这个文件的请求头的请求url,

               

      

      2. 打开url就能看到所有的评论

                      

     

     3. 上代码,解析
    #!/usr/bin/env python# -*- coding: utf-8 -*-
    #
    author tom import requests from lxml import etree headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36' } #抓取函数 def yitianSpiderf(url): res=requests.get(url,headers=headers) tree=etree.HTML(res.content) comment_list=tree.xpath('//d/text()') with open('倚天评论.txt','a+',encoding='utf-8') as f: for comment in comment_list: f.write(comment+' ') #主函数,其实所有是视频找到其id就能抓到所有的弹幕
    def main(): cid='80266814' url='https://api.bilibili.com/x/v1/dm/list.so?oid={}'.format(cid) yitianSpiderf(url) if __name__ == '__main__': main()

       

    4.词云:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #author tom
    import re
    import jieba
    from collections import Counter
    
    #使用结巴分词
    with open('倚天评论.txt','r',encoding='utf-8') as f:
        txt=f.read()
    jbwords=jieba.cut(txt)
    
    #遇到这种非常规词不使用
    with open('中文停用词表.txt' ,'r',encoding='utf-8') as f1:
        stopwords=f1.read()
    result=[]
    for word in jbwords:
        word=re.sub(r'[A-Za-z0-9!?\%[]\,.~]','',word) #去除英文符号
        if word:
            if word not in stopwords:
                result.append(word)
    '+++++++++++++++统计'
    print('=====',result,len(result))
    print(Counter(result))
    
    
    #制作词云图
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud,ImageColorGenerator
    from PIL import Image
    import numpy as np
    
    #指定字体,打开图片,转为数组
    myfon=r'C:WindowsFontssimkai.ttf'
    # img1=Image.open('dog.jpg')
    # graph1=np.array(img1)
    img2=Image.open('1.png')
    graph2=np.array(img2)
    text='/'.join(result)
    #词语对象
    wc=WordCloud(font_path=myfon,background_color='white',max_font_size=50,max_words=500, mask=graph2)
    wc.generate(text)
    img_color=ImageColorGenerator(graph2)#从背景图中生成颜色值
    plt.imshow(wc.recolor(color_func=img_color))
    plt.imshow(wc)
    plt.axis('off')
    plt.show()

      5. 效果:

              

    二.关于B站直播弹幕的爬取

      

    1. 分析发现,b站直播的弹幕存放在一个名为msg的文件当中

        

      2.我们利用postman对这个网站发起post请求,果然可以拿到数据,

        

       3.代码

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #author tom
    import requests
    import time
    from jsonpath import jsonpath
    
    #抓取函数
    def crawl(url,headers,data):
        res=requests.post(url=url,headers=headers,data=data)
        #拿到响应,res.json就直接转化成字典格式了,jsonpath要处理的也需要是一个python字典
        #jsonpath第一个参数是python字典,第二个参数是匹配规则,这边代表的是从根目录递归搜索text和nicname
        comment_list=jsonpath(res.json(),'$..text')
        nicname_list=jsonpath(res.json(),'$..nickname')
        #同时循环两个列表,需要用到zip打包
        for (nicname,comment) in  zip(nicname_list,comment_list):
            dic={
                'nicname':nicname,
                'comment':comment
                 }
            print(dic)
    
    def main():
        url = 'https://api.live.bilibili.com/ajax/msg'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'}
        data = {'roomid': '7734200'}
        #使用while循环最好呀睡觉,哪怕0.1也好,否则内存扛不住
        while True:
            crawl(url,headers,data)
            time.sleep(2)
    
    if __name__ == '__main__':
        main()

    三.b站小视频的爬取

      需求:爬取b站的小视频

      url=url = http://vc.bilibili.com/p/eden/rank#/?tab=全部

    1. 分析:打开这个网址我们发现这是一个ajax请求,并且这个请求里面包含了我们要的小视频地址
      
             
    2.对vedio_playurl请求就直接把视频下载下来了
      
         



    3. 上面的不方便查看,我们就对这个地址发起请求,利用jsonview来帮助查看
           
    
       

      4.看一下请求结果

               

      5.对ajax发起跟过请求

      

      可以看到,带着参数就可以拿到欧更多的数据,现在知道怎么抓去了把

      

  • 相关阅读:
    Manacher-模版题poj3974 hdu3068
    拓展kmp(带注释版)
    颓の第17周
    php 递归遍历目录带缩进
    php 递归遍历目录
    php session
    apache主机配置
    php环境配置的检测方法
    php 变量
    php MVC
  • 原文地址:https://www.cnblogs.com/tjp40922/p/10579406.html
Copyright © 2011-2022 走看看