一.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+'\n') #主函數,其實所有是視頻找到其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:\Windows\Fonts\simkai.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發起跟過請求
可以看到,帶着參數就可以拿到歐更多的數據,現在知道怎么抓去了把