b站每日排行榜爬虫

欢迎关注微信公众号:Smilecoc的杂货铺
回复b站每日排行榜爬虫获取全部源码

爬取B站排行榜前100名的视频名称,作者和播放量,用到的主要有request库获取网页信息,用正则解析网页并使用openpyxl将信息保存在Excel中

第一部分为请求网页获取信息部分,request库的基本用法

def get_html_text(url,self_header):
    try:
        response = requests.get(url,headers=self_header,timeout=30)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return response.text
    except:
        return ""

第二部分为用正则表达式解析网页内容并保存到Excel

def re_get_inf(html):
    list=[]
    rank_list=re.findall(r'<div class="num">(\d*)</div>',html)#排名
    title_list=re.findall(r'<div class="info"><a href=[\s\S]*?class="title">([\s\S]*?)</a><!---->',html)#视频名称
    play_num=re.findall(r'<div class="detail"><span class="data-box"><i class="b-icon play"></i>(\d*.\d*)\S</span>',html)#播放量
    author_list=re.findall(r'<span class="data-box"><i class="b-icon author"></i>([\s\S]*?)</span>',html)#UP主名称
    wb=Workbook()#新建保存文件
    sheet=wb.active
    sheet.append(['rank','title','playnum','author'])#写入标题名称
    for i in range(len(rank_list)):
        rank = rank_list[i]
        title = title_list[i]
        playnum=play_num[i]
        author=author_list[i]
        sheet.append([rank,title,playnum,author])#写入数据
    wb.save('bilibili_rankdata.xlsx')#保存文件

完整代码与注释,结果文件链接:

https://github.com/smilecoc/bilibili_rankdata

个人公众号:Smilecoc的杂货铺,欢迎关注!
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值