【python】下载P站(Pixiv)每日排行榜图片

 佚名文
发布时间:2025-06-30 02:22

本帖最后由 小莫 于 2021-2-22 15:02 编辑

注册好久了,一直在潜水状态,来简单给大家分享一下我之前写的一个下载P站图片的python代码吧;
(python萌新,写的不好还望大佬指点)

使用库:urllib3

代码:

[Python] 纯文本查看 复制代码

# coding:utf-8 import json import os import certifi import urllib3 class getForPixiv: def __init__(self): # 设置爬虫headers self.headers = { "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36", "accept-language": "zh-CN,zh;q=0.9", "referer": "https://www.pixiv.net/", } # 建立连接 self.http = urllib3.PoolManager( cert_reqs='CERT_REQUIRED', ca_certs=certifi.where() ) # 上传文图片到onedrive (如果有装oneindex的话) def upImgToOnedrive(self): p = os.popen('php /www/wwwroot/pan/one.php upload:folder /www/wwwroot/python/img \/images/Pixiv').read() print(p) # 保存图片 def saveImg(self, imgUrl, id): (filePath, imgName) = os.path.split(imgUrl) systemPath = '/www/wwwroot/python/' # 保存路径 dirs = '{systemPath}/img/{id}/'.format(systemPath=systemPath, id=id) if not os.path.exists(dirs): os.makedirs(dirs) path = '{systemPath}/img/{id}/{imgName}'.format(systemPath=systemPath, id=id, imgName=imgName) r = self.http.request('GET', imgUrl, headers=self.headers) with open(path, 'wb') as f: f.write(r.data) r.release_conn() print("\033[32m图片{name}已保存到{path}中.\033[0m".format(name=imgName, path=path)) return imgName # 获取图片列表 def getImgList(self, pid): # https://www.pixiv.net/ajax/illust/87011701/pages?lang=zh url = 'https://www.pixiv.net/ajax/illust/{pid}/pages?lang=zh'.format(pid=pid) r = self.http.request('GET', url, headers=self.headers) jData = json.loads(r.data.decode('utf-8')) body = jData['body'] imgList = [] for val in body: imgList.append({ "original": self.saveImg(val['urls']['original'], pid), "regular": self.saveImg(val['urls']['regular'], pid), "small": self.saveImg(val['urls']['small'], pid), "thumb_mini": self.saveImg(val['urls']['thumb_mini'], pid), }) return imgList def Run(self, url): r = self.http.request('GET', url, headers=self.headers) jData = json.loads(r.data.decode('utf-8')) contents = jData['contents'] dataList = [] for val in contents: dataList.append({ 'title': val['title'], 'illust_id': val['illust_id'], 'tags': val['tags'], 'profile_img': val['profile_img'], 'user_name': val['user_name'], 'user_id': val['user_id'], 'imgList': self.getImgList(val['illust_id']) }) return dataList



调用:

[Python] 纯文本查看 复制代码

fp = getForPixiv() # 参数 p=2 为往前1天, p=3 为往前2天,以此类推 # https://www.pixiv.net/ranking.php?mode=daily&content=illust&p=2&format=json data = fp.Run('https://www.pixiv.net/ranking.php?mode=daily&content=illust&p=1&format=json')



P站图片(不定期更新):
我为了自己方便,直接将图片从服务器上传到我的onedrive网盘了,在线预览,这样我就不用再下载到电脑上,如果你们感兴趣的话,可以来逛逛;
【onedrive没国内服务器,访问可能有点慢(我的服务器也放在海外)】
https://pan.aoe.top/images/Pixiv/



效果:

QQ截图20210220140000.png (174.25 KB, 下载次数: 0)

下载附件

2021-2-20 14:04 上传


QQ截图20210220135931.png (88.83 KB, 下载次数: 1)

下载附件

2021-2-20 14:04 上传




 

点评

白衣国度

竟然不是那个p站,失望  发表于 2021-2-20 18:03

免费评分 参与人数 4吾爱币 +3 热心值 +4 理由
黎明1   + 1   + 1   是这个偶www@pixiv.net  
Titanic     + 1   我心中的P站=pornhub  
suiyunye   + 1   + 1   用心讨论,共获提升!  
godi   + 1   + 1   感谢发布原创作品,吾爱破解论坛因你更精彩!  

查看全部评分

首页
评论
分享
Top