爬取B站热门视频

def get_headers():
“”"
生成响应头
:return: 生成的响应头
“”"
# 随机生成user_agent
user_agent = my_fake_useragent.UserAgent()
ua = str(user_agent.random())
headers = {http://yezishuju.com/
‘user-agent’: ua
}
return headers

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PythonB热门视频信息,需要用到requests和BeautifulSoup库。下面是一些大致步骤: 1. 发送请求获网页源代码 2. 解析网页源代码,获视频信息 3. 存储数据 具体的代码实现可以参考以下步骤: 1. 导入requests和BeautifulSoup库 ``` import requests from bs4 import BeautifulSoup ``` 2. 发送请求获网页源代码 ``` url = 'https://www.bilibili.com/v/popular/rank/all' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) html = response.text ``` 3. 解析网页源代码,获视频信息 ``` soup = BeautifulSoup(html, 'html.parser') video_list = soup.find_all('li', class_='rank-item') for video in video_list: title = video.find('a', class_='title').text.strip() author = video.find('a', class_='name').text.strip() play = video.find('span', class_='data-box').find_all('span').text.strip() danmu = video.find('span', class_='data-box').find_all('span').text.strip() print('标题:', title) print('作者:', author) print('播放量:', play) print('弹幕数:', danmu) ``` 4. 存储数据 可以将数据存储到本地文件中,也可以将数据存储到数据库中。这里以将数据存储到本地文件中为例: ``` with open('bilibili.txt', 'w', encoding='utf-8') as f: for video in video_list: title = video.find('a', class_='title').text.strip() author = video.find('a', class_='name').text.strip() play = video.find('span', class_='data-box').find_all('span').text.strip() danmu = video.find('span', class_='data-box').find_all('span').text.strip() f.write('标题:{}\n作者:{}\n播放量:{}\n弹幕数:{}\n\n'.format(title, author, play, danmu)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值