了解B站最受欢迎的编程课程
到发布博客为止亲测有效!
具体资料在这里,免费的!
网址为https://search.bilibili.com/all?keyword=编程&order=totalrank&duration=0&tids_1=0,
按照totalrank排序,如果希望切换成其他排序,只需要修改 URL 中的 order 参数即可:
最多点击(order=click)
最多弹幕(order=dm)
最多收藏(order=stow)
选择两个筛选条件【60分钟以上】和【科技】:https://search.bilibili.com/all?keyword=编程&order=totalrank&duration=4&tids_1=36,
duration=?全部时长,tids_1=?全部分区。
全部时长(duration=0)
10分钟以下(duration=1)
10-30分钟(duration=2)
30-60分钟(duration=3)
60分钟以上(duration=4)
点击第二页:https://search.bilibili.com/all?keyword=编程&order=totalrank&duration=4&tids_1=36&page=2,
page=?页码,总共有 50 页,每页有 20 个视频,共1k个搜索结果。
import requests
import bs4
import time
def get_input():
keyword = input("请输入关键词:")
pages = int(input("请输入要爬取得页数(1~50):"))
while pages not in range(1, 51):
pages = int(input("请输入正确的页数:"))
return keyword, pages
def get_html(url):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
res = requests.get(url, headers=headers)
#print(res)
return res.text
def get_datas(text):
soup = bs4.BeautifulSoup(text, "html.parser")
datas = []
#videos = soup.find_all("li", class_="video matrix")
#videos = soup.find_all("li", class_="video-list-item")
videos = soup.find_all("li", class_="video-item matrix")
for video in videos:
# 获取标题
datas.append(video.a['title'])
# 获取URL
datas.append(video.a['href'])
# 获取观看数/弹幕数/上传时间/阿婆主
# b站现在改版了,要注意一下,不要复制粘贴,
# 在"video-item matrix"中的才是文本,
# 原来的那video matrix已经不存在了
# bili-video-card__stats--icon
tags = video.select("div[class='tags'] > span")
for tag in tags:
datas.append(''.join(tag.text.split()))
return datas
def grouped(iterable, n):
"将列表切成n片一组"
"s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."
return zip(*[iter(iterable)] * n)
def main():
keyword, pages = get_input()
order = ['totalrank', 'click', 'dm', 'stow']
order_name = ['综合排序', '最多点击', '最多弹幕', '最多收藏']
# 迭代每种排序
for i in range(4):
index = 1
# 迭代每一页
for page in range(1, pages + 1):
# url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&tids_1=36&page={}".format(keyword,
# order[i],
# page)
url = "https://search.bilibili.com/all?keyword={}&order={}&duration=4&page={}".format(keyword,
order[i],
page)
text = get_html(url)
datas = get_datas(text)
# 为每种排序创建一个文本文件单独存放
with open(order_name[i] + '.txt', 'a', encoding="utf-8") as file:
for video_title, video_URL, video_watch, video_dm, video_time, video_up in grouped(datas, 6):
file.write(' + '.join(
[str(index), video_title, video_URL, video_watch, video_dm, video_time, video_up, '\n']))
index += 1
# 做一只善意的爬虫,不要给服务器带来负担
time.sleep(1)
if __name__ == "__main__":
main()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?