Ai
1 Star 0 Fork 0

张奕博/jin222

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
张奕博 提交于 2021-06-19 13:07 +08:00 . Initial commit
import csv
import requests
from bs4 import BeautifulSoup
import datetime
url='https://www.bilibili.com/v/popular/rank/all'
#发起网络请求
response=requests.get(url)
html_text=response.text
soup=BeautifulSoup(html_text,'html.parser')
class Video:
def __init__(self,rank,title,score,see,up,up_id,url):
self.rank=rank
self.title=title
self.score=score
self.see=see
self.up=up
self.up_id=up_id
self.url=url
def to_csv(self):
return [self.rank,self.title,self.score,self.see,self.up,self.up_id,self.url]
@staticmethod
def csv_title():
return['排名','标题','分数','播放量','up主','upID','URL']
items=soup.findAll('li',{'class':'rank-item'})
vidoes=[]
for itm in items:
title=itm.find('a',{'class':'title'}).text
rank = itm.find('div', {'class': 'num'}).text
score = itm.find('div', {'class': 'pts'}).find('div').text
see = itm.find('span', {'class': 'data-box'}).text
up = itm.find_all('a')[2].text
up_id= itm.find_all('a')[2].get('href')[len('//space.bilibili.com/'):]
url = itm.find('a', {'class': 'title'}).get('href')
v=Video(rank,title,score,see,up,up_id,url)
vidoes.append(v)
now_str=datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
file_name=f'top100_{now_str}.csv'
with open(file_name,'w',newline='',encoding='utf-8') as f:
pen=csv.writer(f)
pen.writerow(Video.csv_title())
for v in vidoes:
pen.writerow(v.to_csv())
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhang-yibo/jin222.git
git@gitee.com:zhang-yibo/jin222.git
zhang-yibo
jin222
jin222
master

搜索帮助