python爬取bilibili网页排名,视频,播放量,点赞量,链接等内容并存储csv文件中

Roselani ·
更新时间:2024-09-21
· 495 次阅读

首先要了解html标签,标签有主有次,大致了解以一下,主标签是根标签,也是所有要爬取的标签的结合体

先了解一下待会要使用代码属性:

#获取属性 a.attrs  获取a所有的属性和属性值,返回一个字典 a.attrs['href']   获取href属性 a['href']   也可简写为这种形式 #获取内容 a.string      获取a标签的直系文本 注意:如果标签还有标签,那么string获取到的结果为None,而其它两个,可以获取文本内容 a.text     这是属性,获取a子类的所有文本 a.get_text()  这是方法,获取a标签子类的所有文本 #find   主要用于找到第一个符合要求的标签 a.find('a')              找到第一个符合要求的 a.find('a', title="xxx") 具有title=a属性的 a.find('a', alt="xxx") a.find('a', class_="xxx") a.find('a', id="xxx") #find_all  用于找到所有符合要求的标签 a.find_all('a') a.find_all(['a','b']) 找到所有的a和b标签 a.find_all('a', limit=2)  限制前两个 #根据选择器选择指定的内容 a.select('#feng')

我们今天要爬取的是bilibili网站,主要是作为一个练习,也没有太主要的作用,爬取的内容分别是

排名、视频名、视频集数、播放量、点赞量及视频链接,爬取完后要存储到csv文件内

 直接上代码:

# -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup r = requests.get('https://www.bilibili.com/v/popular/rank/guochan')   #要爬取的网站链接 html = r.content soup = BeautifulSoup(html,'html.parser')    #html.parser是解析器 div_people_list = soup.find('div', attrs={'class': 'rank-list-wrap'}) div_people_list_list = div_people_list.find('ul', attrs={'class': 'rank-list pgc-list'}) a_s = div_people_list.find_all('li', attrs={'class': 'rank-item'}) for a in a_s:    #排名     for b in a.find_all('div', attrs={'class': 'info'}):         for c in b.find_all('a', attrs={'target': '_blank'}):     #名称及链接             for d in b.find('span', attrs={'class': 'data-box'}):   #获取视频集                 for e in b.find_all('span', attrs={'class': 'data-box'})[1:][:1]:  #循环播放数                     for f in b.find_all('span', attrs={'class': 'data-box'})[2:][:2]:  # 循环点赞量                         web = a['data-rank']  # 排名                         name = c.string  # 名称                         name_2 = d.string  # 全集                         name_2_1 = name_2.replace(" ", "").replace("\t", "").strip()  # 去除多余空格                         name_3 = e.get_text()  # 播放量                         data_1 = name_3.replace(" ", "").replace("\n", "").replace("\t", "")                         name_4 = f.get_text()  # 点赞量                         data_2 = name_4.replace(" ", "").replace("\n", "").replace("\t", "")                         url = c['href']  # 链接                         print(web + '\t' + name + '\t\t\t' + name_2_1 + '\t\t\t\t' + data_1 + '\t\t\t\t'+ data_2+'\t\t\t\t' + f'http:{url}')

效果如下:

其实我在做一个爬虫的时候遇到很多的错误,就是html里的标签重复,需要使用[1:][:1]来选择标签,有的时候不是难,就是项目做的少

 我们现在来加上这个代码,把爬取的内容保存下来:

import csv f = open('爬取文件.csv','w',encoding='gbk',newline='')  #写入文件 #基于文件对象构建 csv写入对象 csv_writer = csv.writer(f) csv_writer.writerow(['排行','影片', '篇集','播放量','点赞量','视频链接']) csv_writer.writerow([web, name, n,d,g,f'http:{url}'])

可以看到内容都存储到了csv文件内了,不是很乱,和刚刚dos窗口里的相比好了很多

完整代码如下:

# -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup import csv f = open('爬取文件.csv','w',encoding='gbk',newline='')  #写入文件 #基于文件对象构建 csv写入对象 csv_writer = csv.writer(f) csv_writer.writerow(['排行','影片', '篇集','播放量','点赞量','视频链接']) r = requests.get('https://www.bilibili.com/v/popular/rank/guochan')   #要爬取的网站链接 html = r.content soup = BeautifulSoup(html,'html.parser')    #html.parser是解析器 div_people_list = soup.find('div', attrs={'class': 'rank-list-wrap'}) div_people_list_list = div_people_list.find('ul', attrs={'class': 'rank-list pgc-list'}) a_s = div_people_list.find_all('li', attrs={'class': 'rank-item'}) #a_s_2 = a_s.find_all('div', attrs={'class': 'info'}) for a in a_s:    #排名     for b in a.find_all('div', attrs={'class': 'info'}):         for c in b.find_all('a', attrs={'target': '_blank'}):     #名称及链接             for d in b.find('span', attrs={'class': 'data-box'}):   #获取视频集                 for e in b.find_all('span', attrs={'class': 'data-box'})[1:][:1]:  #循环播放数                     for f in b.find_all('span', attrs={'class': 'data-box'})[2:][:2]:  # 循环点赞量                         web = a['data-rank']  # 排名                         name = c.string  # 名称                         name_2 = d.string  # 全集                         name_2_1 = name_2.replace(" ", "").replace("\t", "").strip()  # 去除多余空格                         name_3 = e.get_text()  # 播放量                         data_1 = name_3.replace(" ", "").replace("\n", "").replace("\t", "")                         name_4 = f.get_text()  # 点赞量                         data_2 = name_4.replace(" ", "").replace("\n", "").replace("\t", "")                         url = c['href']  # 链接                         n = name_2.replace(" ", "").replace("\t", "").strip()  # 去除多余空格                         d = name_3.replace(" ", "").replace("\t", "")                         g = name_4.replace(" ", "").replace("\t", "")                         #构建列表头                         csv_writer.writerow([web, name, n,d,g,f'http:{url}'])

到此这篇关于python爬取bilibili网页排名,视频,播放量,点赞量,链接等内容并存储csv文件中的文章就介绍到这了,更多相关python爬取bilibili内容并存储csv文件中内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



csv文件 bilibili csv Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号