生成代码python3爬取b站热门视频
时间: 2023-09-30 17:09:51 浏览: 111
好的,以下是可以使用的Python 3代码,可以爬取B站热门视频:
import requests
from bs4 import BeautifulSoup
url = 'https://www.bilibili.com/v/popular/rank/all'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
videos = soup.find_all('li', class_='rank-item')
for video in videos:
title = video.find('a', class_='title').text.strip()
link = 'https:' + video.find('a', class_='title')['href']
play_count = video.find('span', class_='data-box').text.strip()
print('标题:', title)
print('链接:', link)
print('播放量:', play_count)
print('-----------------------')
这个代码使用了 requests 库来发送 HTTP 请求,并使用了 BeautifulSoup 库来解析 HTML 页面。它会从 B 站热门排行榜页面中获取视频的标题、链接和播放量,并将这些信息打印到控制台中。
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)