上一篇对b站的视频评论爬取进行了探讨,这一篇是弹幕。直接上代码:
- import csv
- import json
- import re
- import chardet
- import requests
-
- headers = {
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.3162 SLBChan/105'}
-
-
- # 对爬取的页面内容进行json格式处理
- def get_text(url):
- res = requests.get(url=url, headers=headers)
- res.encoding = chardet.detect(res.content)['encoding'] # 统一字符编码
- res = res.text
- data = json.loads(res) # json格式化
- return data
-
-
- def get_cid(bv):
- url_1 = 'https://api.bilibili.com/x/player/pagelist?bvid={}'.format(bv)
- response = get_text(url_1)
- cid = response['data'][0]['cid'] # 获取cid
- return cid
-
-
- def get_content_list(cid):
- content_list = []
- url = f'https://comment.bilibili.com/{cid}.xml'
- r2 = requests.ge