如何使用python爬取B站排行榜Top100的视频数据

记得收藏呀!!!
1、第三方库导入

from bs4 import BeautifulSoup # 解析网页import re# 正则表达式,进行文字匹配import urllib.request,urllib.error# 通过浏览器请求数据import sqlite3# 轻型数据库import time# 获取当前时间

2、程序运行主函数 爬取过程主要包括声明爬取网页 -> 爬取网页数据并解析 -> 保存数据
def main(): #声明爬取网站baseurl = "https://www.bilibili.com/v/popular/rank/all"#爬取网页datalist = getData(baseurl)# print(datalist)#保存数据dbname = time.strftime("%Y-%m-%d", time.localtime())dbpath = "BiliBiliTop100" + dbnamesaveData(datalist,dbpath)

(1)在爬取的过程中采用的技术为:伪装成浏览器对数据进行请求;
(2)解析爬取到的网页源码时:采用Beautifulsoup解析出需要的数据,使用re正则表达式对数据进行匹配;
(3)保存数据时,考虑到B站排行榜是每日进行刷新,故可以用当前日期进行保存数据库命名。
3、程序运行结果 如何使用python爬取B站排行榜Top100的视频数据
文章图片

数据库中包含的数据有:排名、视频链接、标题、播放量、评论量、作者、综合分数这7个数据。
如何使用python爬取B站排行榜Top100的视频数据
文章图片

4、程序源代码
from bs4 import BeautifulSoup #解析网页import re # 正则表达式,进行文字匹配import urllib.request,urllib.errorimport sqlite3import timedef main():#声明爬取网站baseurl = "https://www.bilibili.com/v/popular/rank/all"#爬取网页datalist = getData(baseurl)# print(datalist)#保存数据dbname = time.strftime("%Y-%m-%d", time.localtime())dbpath = "BiliBiliTop100" + dbnamesaveData(datalist,dbpath)#re正则表达式findLink =re.compile(r'(.*?)') #榜单次序findTitle = re.compile(r'(.*?)') #视频标题findPlay = re.compile(r'([\s\S]*)(.*?) ') #视频播放量findView = re.compile(r'([\s\S]*)(.*?) ') # 视频评价数findName = re.compile(r'(.*?)',re.S) #视频作者findScore = re.compile(r'(.*?)综合得分',re.S) #视频得分def getData(baseurl):datalist = []html = askURL(baseurl)#print(html)soup = BeautifulSoup(html,'html.parser')#解释器for item in soup.find_all('li',class_="rank-item"):# print(item)data = https://www.it610.com/article/[]item = str(item)Order = re.findall(findOrder,item)[0]data.append(Order)# print(Order)Link = re.findall(findLink,item)[0]Link ='https:' + Linkdata.append(Link)# print(Link)Title = re.findall(findTitle,item)[0]data.append(Title)# print(Title)Play = re.findall(findPlay,item)[0][0]Play = Play.replace(" ","")Play = Play.replace("\n","")Play = Play.replace(".","")Play = Play.replace("万","0000")data.append(Play)# print(Play)View = re.findall(findView,item)[0][0]View = View.replace(" ","")View = View.replace("\n","")View = View.replace(".","")View = View.replace("万","0000")data.append(View)# print(View)Name = re.findall(findName,item)[0]Name = Name.replace(" ","")Name = Name.replace("\n","")data.append(Name)# print(Name)Score = re.findall(findScore,item)[0]data.append(Score)# print(Score)datalist.append(data)return datalistdef askURL(url):#设置请求头head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/80.0.3987.163Safari/537.36"}request = urllib.request.Request(url, headers = head)html = ""try:response = urllib.request.urlopen(request)html = response.read().decode("utf-8")#print(html)except urllib.error.URLError as e:if hasattr(e,"code"):print(e.code)if hasattr(e,"reason"):print(e.reason)return htmldef saveData(datalist,dbpath):init_db(dbpath)conn = sqlite3.connect(dbpath)cur = conn.cursor()for data in datalist:sql = '''insert into Top100(id,info_link,title,play,view,name,score)values("%s","%s","%s","%s","%s","%s","%s")'''%(data[0],data[1],data[2],data[3],data[4],data[5],data[6])print(sql)cur.execute(sql)conn.commit()cur.close()conn.close()def init_db(dbpath):sql = '''create table Top100(id integer primary key autoincrement,info_link text,title text,play numeric,view numeric,name text,score numeric)'''conn = sqlite3.connect(dbpath)cursor = conn.cursor()cursor.execute(sql)conn.commit()conn.close()if __name__ =="__main__":main()

【如何使用python爬取B站排行榜Top100的视频数据】到此这篇关于如何使用python爬取B站排行榜Top100的视频数据的文章就介绍到这了,更多相关python B站视频 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读