卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

爬虫|Python爬取B站小姐姐图片,学习的动力!


本期给大家介绍如何用Python爬取B站小姐姐图片,希望对你有所帮助。


1. 网页分析

直接打开B站(bilibili)搜索 '小姐姐':

爬虫|Python爬取B站小姐姐图片,学习的动力!
一共有5页内容,以第2页为例,F12打开网页源码:

爬虫|Python爬取B站小姐姐图片,学习的动力!

搜索第一个title,我们可以找到相应的XHR请求,仔细分析一下发现所有的数据都存在于一个json格式的数据集里,我们的目标就在result列表中。

检查Headers如下:

爬虫|Python爬取B站小姐姐图片,学习的动力!


这是一个get请求,请求参赛数中有pagekeyword两个参赛,分别对应请求的页码和关键字。

多查看几页找一下规律:

# 第一页
'https://api.bilibili.com/x/web-interface/search/all/v2?context=&page=1&order=totalrank&keyword=%E5%B0%8F%E5%A7%90%E5%A7%90&duration=0&tids_2=&from_source=&from_spmid=333.337&platform=pc&__refresh__=true&_extra=&tids=0&highlight=1&single_column=0'
# 第二页
'https://api.bilibili.com/x/web-interface/search/type?context=&page=2&order=totalrank&keyword=%E5%B0%8F%E5%A7%90%E5%A7%90&duration=0&tids_2=&from_source=&from_spmid=333.337&platform=pc&__refresh__=true&_extra=&search_type=video&tids=0&highlight=1&single_column=0'
# 第三页
'https://api.bilibili.com/x/web-interface/search/type?context=&page=3&order=totalrank&keyword=%E5%B0%8F%E5%A7%90%E5%A7%90&duration=0&tids_2=&from_source=&from_spmid=333.337&platform=pc&__refresh__=true&_extra=&search_type=video&tids=0&highlight=1&single_column=0'


可以看到除了第1页不一样外,其他几页url中只有page参数不一样,那么我们尝试一下第1页也用其他页的url请求一下,结果会发现同样可以出来想要的结果(自己试试)。

结论:所有页url只有page参数不一样,其他均一致。


2. 数据爬取

2.1 导入模块
# 导包
import re
import time
import json
import random
import requests
from fake_useragent import UserAgent

2.2 获取页面信息

根据分析的url请求数据:
# 获取页面信息
def get_datas(url,headers):
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    r.encoding = chardet.detect(r.content)['encoding'] 
    datas = json.loads(r.text)
    return datas
2.3 获取具体图片信息
# 获取图片链接信息
def get_hrefs(datas):
    titles,hrefs = [],[]
    for data in datas['data']['result']:
        # 标题
        title = data['title']
        # 时长
        duration = data['duration']
        # 播放量
        video_review =data['video_review']
        # 发布时间
        date_rls = data['pubdate']
        pubdate = time.strftime('%Y-%m-%d %H:%M', time.localtime(date_rls))
        # 作者
        author = data['author']
        # 图片链接
        link_pic = data['pic']
        href_pic = 'https:' + link_pic
        
        titles.append(title)
        hrefs.append(href_pic)
        
        return titles, hrefs

代码解析了视频标题,时长,播放量,发布时间,作者,图片链接等参数,这里我们只取标题图片链接,其他参数可根据需要自行增,删。

2.4 保存图片
# 保存图片
def download_jpg(titles, hrefs):
    path = "D:/B站小姐姐/"
    if not os.path.exists(path):
        os.mkdir(path)
    for i in range(len(hrefs)):
        title_t = titles[i].replace('/','').replace(',','').replace('?','')
     title_t = title_t.replace(' ','').replace('|','').replace('。','')
        filename = '{}{}.jpg'.format(path,title_t)
        with open(filename, 'wb') as f:
            req = requests.get(url=hrefs[i], headers=headers)
            f.write(req.content)
            time.sleep(random.uniform(1.5,3.4))
这里我们用标题作为图片名称进行存储,需要注意文件名称不能包含特殊符号,这里过滤了” / ,。|“等4种(每天视频有增删,可能有出入,需要自己调整,也可以不使用标题做名称)。


3. 结果

部分图片:

爬虫|Python爬取B站小姐姐图片,学习的动力!


卓越飞翔博客
上一篇: Python实现无头浏览器采集应用的页面模拟点击与滚动功能解析
下一篇: 技巧 | 99.9%的人都会犯错的几个Python小常识!
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏