如何用Python爬虫高效获取B站美女图片,激发学习热情?

更新于
2026-10-11 15:38:55
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2688个文字,预计阅读时间需要11分钟。

如何用Python爬虫高效获取B站美女图片,激发学习热情?

本期间为您介绍如何使用Python爬取B站小姐姐图片,希望对您有所帮助。

1. 网页分析直接打开B站(bilibili)搜索小姐姐:共找到5页内容,以第2页为例(F12打开网页源代码):搜索第一个小姐姐的链接。

2. 爬取小姐姐图片pythonimport requestsfrom bs4 import BeautifulSoup

def get_images(url): headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } response=requests.get(url, headers=headers) soup=BeautifulSoup(response.text, '.parser') images=soup.find_all('img') for img in images: img_url=img.get('data-src') if img_url: img_response=requests.get(img_url, headers=headers) with open('image.jpg', 'wb') as f: f.write(img_response.content) print('图片已保存:image.jpg')

if __name__=='__main__': url='https://search.bilibili.com/all?keyword=小姐姐&page=2' get_images(url)

运行上述代码,即可爬取B站第2页小姐姐的图片。


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


1. 网页分析

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

一共有5页内容,以第2页为例,F12打开网页源码:

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

检查Headers如下:


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

如何用Python爬虫高效获取B站美女图片,激发学习热情?

多查看几页找一下规律:

# 第一页 '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' # 第二页 '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' # 第三页 '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. 结果

部分图片:


标签:动力

本文共计2688个文字,预计阅读时间需要11分钟。

如何用Python爬虫高效获取B站美女图片,激发学习热情?

本期间为您介绍如何使用Python爬取B站小姐姐图片,希望对您有所帮助。

1. 网页分析直接打开B站(bilibili)搜索小姐姐:共找到5页内容,以第2页为例(F12打开网页源代码):搜索第一个小姐姐的链接。

2. 爬取小姐姐图片pythonimport requestsfrom bs4 import BeautifulSoup

def get_images(url): headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } response=requests.get(url, headers=headers) soup=BeautifulSoup(response.text, '.parser') images=soup.find_all('img') for img in images: img_url=img.get('data-src') if img_url: img_response=requests.get(img_url, headers=headers) with open('image.jpg', 'wb') as f: f.write(img_response.content) print('图片已保存:image.jpg')

if __name__=='__main__': url='https://search.bilibili.com/all?keyword=小姐姐&page=2' get_images(url)

运行上述代码,即可爬取B站第2页小姐姐的图片。


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


1. 网页分析

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

一共有5页内容,以第2页为例,F12打开网页源码:

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

检查Headers如下:


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

如何用Python爬虫高效获取B站美女图片,激发学习热情?

多查看几页找一下规律:

# 第一页 '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' # 第二页 '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' # 第三页 '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. 结果

部分图片:


标签:动力