python爬取b站网页信息,Python网页爬虫练习:requests库&Beautiful爬取bilibili网页信息...

本文介绍了两个 Python 函数:get_bilibili_vedio 和 get_bilibili_firstpage_vedio,分别用于获取 bilibili 实时热榜视频的排名、链接和标题,以及网页首页推荐视频的信息。通过BeautifulSoup库解析网页内容,展示了如何使用requests库进行网络请求并处理HTTP错误。
摘要由CSDN通过智能技术生成

我是卢本伟!

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值