一、网页信息(示例网址:https://space.bilibili.com/3493110839511225/video)
二、查看响应数据
三、查看数据包内容
四、相关代码(代码内容未进行翻页爬取)
# @Time: 2024/1/19 16:42
# @Author: 马龙强
# @File: 采集B站up主信息.py
# @software: PyCharm
"""
网址:https://space.bilibili.com/3493110839511225/video
数据:标题、BV号、播放量、评论、弹幕。。。。
数据包:https://api.bilibili.com/x/space/wbi/arc/search
代码步骤:发送请求、获取数据、解析数据、保存数据
多页数据采集 -> 采集up所有视频信息数据
分析链接变化规律
url中:
- pn :页码
- dm_img_list :鼠标移动数据
- w_rid :MD5加密参数
- wts :时间戳
发生变化
"""
import requests
import datetime #导入时间模块
import csv
"""创建文件对象"""
f = open('视频信息.csv',mode='w',encoding='utf-8',newline='')
"""
:.*
,
"""
csv_writer = csv.DictWriter(f,fieldnames=[
'标题',
'播放量',
'评论',
'弹幕',
'时长',
'bv号',
'日期',
'上传时间',
])
csv_writer.writeheader()
#模拟浏览器
headers = {
"Cookie": "buvid3=94A5BDE1-CDDC-12C9-0FD4-3305DE8DCE8D76087infoc; b_nut=1699609476; i-wanna-go-back=-1; b_ut=7; _uuid=39D4B639-9B3B-D881-3D23-3998B94CC4C677381infoc; enable_web_push=DISABLE; buvid4=EFEE2640-75A3-B07D-5AFC-09D951D5581877370-023111017-YCb7ryWqJw+YFnyAAbCP2w%3D%3D; home_feed_column=5; browser_resolution=1536-746; buvid_fp=25ba4d2dc2d9aa5a126e6089b07107f2; CURRENT_FNVAL=4048; rpdid=|(J~umJRk|Yl0J'uYmmY|mR)); header_theme_version=CLOSE; sid=4hrgjlgl; PVID=1; b_lsid=45510CAB2_18D20E83DF5; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDU5MTMzMDcsImlhdCI6MTcwNTY1NDA0NywicGx0IjotMX0.IYXXtNtqUAEUWNSZrLv_iFUYwmYj-ml3PrjnDLtk_Q4; bili_ticket_expires=1705913247",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0"
}
#请求网址
"""
请求网址较长进行分段写:
问号前面:链接地址
问号后面:查询参数(请求参数)
批量替换方法:
(1)选择需要被替换的内容,ctrl+R
(2)勾选 .*
(3)使用正则替换命令
(.*?):(.*)
"$1":"$2",
"""
url = 'https://api.bilibili.com/x/space/wbi/arc/search?mid=3493110839511225&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true&dm_img_list=[%7B%22x%22:1761,%22y%22:324,%22z%22:0,%22timestamp%22:84,%22type%22:0%7D,%7B%22x%22:1609,%22y%22:444,%22z%22:44,%22timestamp%22:197,%22type%22:0%7D,%7B%22x%22:1635,%22y%22:483,%22z%22:100,%22timestamp%22:341,%22type%22:0%7D,%7B%22x%22:1678,%22y%22:618,%22z%22:74,%22timestamp%22:445,%22type%22:0%7D]&dm_img_str=V2ViR0wgMS4wIChPcGVuR0wgRVMgMi4wIENocm9taXVtKQ&dm_cover_img_str=QU5HTEUgKEludGVsLCBJbnRlbChSKSBVSEQgR3JhcGhpY3MgRGlyZWN0M0QxMSB2c181XzAgcHNfNV8wLCBEM0QxMSlHb29nbGUgSW5jLiAoSW50ZW&w_rid=c7ac0e5f775a43c5784ae18cdb074efb&wts=1705737812'
#发送请求
response = requests.get(url=url,headers=headers)
"""获取数据:获取服务器返回响应数据
- response.text 文本
- response.json() json
- response.content 获取相应二进制数据
保存数据:图片、音频、视频、特定格式文件
"""
#解析数据
# print(response.text) #字符串
# print(response.json()) #字典
"""
键值对取值:根据冒号左边的内容[键],提取冒号右边的内容[值]
dit = {'A':'a','B':{'B1':'b'},'C':{'C1':[{'C2':'c2'}]}}
提取b -> dit['B']['B1']
提取c2 -> dit[][]
"""
json_data = response.json()
info_list = json_data['data']['list']['vlist']
#for循环遍历
for index in info_list:
#上传时间戳
# data_time = index['created']
data_time = str(datetime.datetime.fromtimestamp(index['created']))
date = data_time.split(' ')[0] #日期
up_time = data_time.split(' ')[1] #上传时间
"""提取具体数据内容"""
dit = {
'标题':index['title'],
'播放量':index['play'],
'评论':index['comment'],
'弹幕':index['video_review'],
'时长':index['length'],
'bv号':index['bvid'],
'日期':date,
'上传时间':up_time
}
#写入数据
csv_writer.writerow(dit)
print(dit)
五、爬取内容