我是卢本伟!
import requests
from bs4 import BeautifulSoup
import bs4
def get_blibli_vedio():#获取bilibili实时热榜视频排行榜
url = "https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3"
try:
r = requests.get(url, timeout=50)
r.raise_for_status()
r.encoding = r.apparent_encoding
soup=BeautifulSoup(r.text,"html.parser")
get_contents=[]
for tag_li in soup.find(name="ul",attrs={"class","rank-list"}).children:
if isinstance(tag_li,bs4.element.Tag):#过滤非Tag类型实例
one_vedio={}
one_vedio["当前排名"]=int(tag_li.find(name="div",attrs={"class","num"}).string)
one_vedio["视频链接"]=tag_li.find(name="a").attrs["href"]
one_vedio["视频标题"]=tag_li.find(name="a",attrs={"class","title"}).string
get_contents.append(one_vedio)
for each_vedio in get_contents:
print("当前排名:"+str(each_vedio["当前排名"])+" "+"视频标题:"+each_vedio["视频标题"]+" "+"视频链接"+each_vedio["视频链接"])
except requests.HTTPError:
print("HTTPError")
def get_bilibili_firstpage_vedio():#爬取bilibili网页首页的视频信息
url="https://www.bilibili.com/?spm_id_from=333.851.b_696e7465726e6174696f6e616c486561646572.1"
try:
r = requests.get(url, timeout=50)
r.raise_for_status()
r.encoding = r.apparent_encoding
soup=BeautifulSoup(r.text,"html.parser")
get_contents=[]
for each_firstpage_vedio in soup.find(name="div",attrs={"class","recommend-box"}).children:
if isinstance(each_firstpage_vedio,bs4.element.Tag):#过滤非标签
if each_firstpage_vedio.attrs["class"][0]=="video-card-reco":
each_vedio=[]
each_vedio.append(each_firstpage_vedio.find(name="a").attrs["href"])
each_vedio.append(each_firstpage_vedio.find(name="p",attrs={"class","title"}).string)
for e in each_firstpage_vedio.find(name="p", attrs={"class", "up"}).descendants:
if isinstance(e,bs4.element.NavigableString):
each_vedio.append(e)
get_contents.append(each_vedio)
for each in get_contents:
print("视频链接:" +each[0]+" "+"视频标题:" +each[1]+" "+"up主:"+each[2])
except requests.HTTPError:
print("HTTPError")
if __name__=="__main__":
get_blibli_vedio()
get_bilibili_firstpage_vedio()