爬取哔哩哔哩每日热榜

您所在的位置:网站首页 哔哩哔哩热搜榜怎么关掉 爬取哔哩哔哩每日热榜

爬取哔哩哔哩每日热榜

2024-07-11 18:00| 来源: 网络整理| 查看: 265

import requestsfrom bs4 import BeautifulSoupimport csvimport datetimeurl = 'https://www.bilibili.com/ranking?'#发起网络请求response = requests.get(url)html_text = response.textsoup = BeautifulSoup(html_text,'html.parser')def main(): #用来保存视频信息的对象 class Video: def __init__(self,rank,title,point,visit,up,url): self.rank = rank self.title = title self.point = point self.visit = visit self.up = up self.url = url def to_csv(self): return [self.rank,self.title,self.point,self.visit,self.up,self.url]

@staticmethod def csv_title(): return ['排名','标题','分数','播放量','UP','URL'] #提取列表 items = soup.find_all('li',{'class':'rank-item'}) videos = [] #保存提取出来的video for itm in items: title = itm.find('a',{'class':'title'}).text#标题 point = itm.find('div',{'class':'pts'}).text#综合得分 rank = itm.find('div',{'class':'num'}).text#排名 visit = itm.find('span',{'class':'data-box'}).text#播放量 up = itm.find_all('a',)[2].text#up url = itm.find('a',{'class':'title'}).get('href')#获取链接 v = Video(rank,title,point,visit,up,url) videos.append(v)

#保存 now_str = datetime.datetime.now().strftime('%Y%m%d') file_name = f'top100.csv_{now_str}.csv' with open(file_name,'w',newline='') as f: writer = csv.writer(f) writer.writerow(Video.csv_title()) for v in videos: writer.writerow(v.to_csv())



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3