Python爬虫实战:获取Bilibili各分区视频播放量
import requests
from bs4 import BeautifulSoup
# 定义要爬取的视频分区
video_types = ['动画', '音乐', '游戏', '娱乐', '科技', '生活', '鬼畜', '时尚', '广告', '电影', '电视剧', '纪录片', '影视', '国创']
# 循环爬取每个分区的视频播放量
for video_type in video_types:
# 构造url
url = f'https://search.bilibili.com/all?keyword={video_type}&order=click&duration=0&tids_1=0&page=1'
# 发送请求
response = requests.get(url)
# 解析html
soup = BeautifulSoup(response.text, 'html.parser')
# 获取视频列表
video_list = soup.find_all('li', class_='video-item matrix')
# 循环爬取每个视频的播放量
for video in video_list:
# 获取视频链接
video_link = video.find('a', class_='title').get('href')
# 构造视频链接
video_url = 'https:' + video_link
# 发送请求
video_response = requests.get(video_url)
# 解析html
video_soup = BeautifulSoup(video_response.text, 'html.parser')
# 获取视频播放量
video_view = video_soup.find('span', class_='view').text
# 输出结果
print(f'{video_type} 分区的视频 {video_link} 的播放量为:{video_view}')
原文地址: https://www.cveoy.top/t/topic/g58B 著作权归作者所有。请勿转载和采集!