吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1806|回复: 28
上一主题 下一主题
收起左侧

[Python 原创] 【Pixiv】已收藏插画批量下载

  [复制链接]
跳转到指定楼层
楼主
梦汐 发表于 2025-3-13 11:15 回帖奖励
需要登陆才能使用,登陆方法是填写COOKIE
import json
import os
import requests
import concurrent.futures
from urllib.parse import urlparse

# 配置信息
headers = {
    "cookie": "", # 填写账号Cookie里的PHPSESSID或全部键值对
    "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)",
}
user_id = "" # 填写PHPSESSID前面的数字
proxies = None  # {"https": None}
bookmarks = {}  # 收藏列表
history = {}  # 下载历史

# 初始化文件
if not os.path.exists("bookmarks.json"):
    with open("bookmarks.json", "w") as f:
        f.write("{}")
if not os.path.exists("history.json"):
    with open("history.json", "w") as f:
        f.write("{}")
bookmarks = json.loads(open("bookmarks.json").read())
history = json.loads(open("history.json").read())

# 检查登录状态
def check_login_status():
    try:
        response = requests.get(
            "https://www.pixiv.net/ajax/my_profile", headers=headers, proxies=proxies
        )
        return not response.json()["error"]
    except:
        return False

# 更新收藏列表
def update_bookmarks():
    offset = 0
    limit = 100
    total = None
    incremental = []
    repeat_count = 0

    while True:
        print(f"Loading page {int((offset + limit) / limit)}")
        url = f"https://www.pixiv.net/ajax/user/{user_id}/illusts/bookmarks?tag=&offset={offset}&limit={limit}&rest=show&lang=zh"
        data = requests.get(url, headers=headers, proxies=proxies).json()

        if total is None:
            total = data["body"]["total"]

        for bookmark in data["body"]["works"]:
            if bookmark["id"] not in bookmarks:
                bookmarks[bookmark["id"]] = bookmark
                incremental.append(bookmark["id"])
                repeat_count = 0  # 重置计数器
                print(f"New illustration: {bookmark['id']}")
            else:
                repeat_count += 1  # 增加计数器
                if repeat_count >= 10:  # 检查是否达到10个连续重复
                    print("跳出循环")  # 连续重复
                    break

        offset += limit
        if incremental:
            print("...ok")
        else:
            print("No new illustrations")

        if offset >= total or repeat_count >= 10:
            break

    total_bookmarks = len(bookmarks)
    print(f"Total bookmarks: {total_bookmarks}")
    with open("bookmarks.json", "w") as f:
        json.dump(bookmarks, f, indent=4)
    return bookmarks

# 获取插画数据
def fetch_illustration_data():
    def get_picture(pid):
        if "picture" not in bookmarks[pid]:
            url = f"https://www.pixiv.net/ajax/illust/{pid}/pages"
            bookmarks[pid]["picture"] = requests.get(
                url, headers=headers, proxies=proxies
            ).json()
            return pid
        return None

    with concurrent.futures.ThreadPoolExecutor(max_workers=25) as executor:
        futures = {executor.submit(get_picture, pid): pid for pid in bookmarks}
        for i, future in enumerate(concurrent.futures.as_completed(futures)):
            pid = futures[future]
            try:
                result = future.result()
                if result and i % 50 == 0:
                    with open("bookmarks.json", "w") as f:
                        json.dump(bookmarks, f, indent=4)
            except Exception as e:
                print(f"Error fetching data for {pid}: {e}")

    with open("bookmarks.json", "w") as f:
        json.dump(bookmarks, f, indent=4)

# 下载图片
def download_image(url, path):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)",
        "origin": "https://www.pixiv.net/",
        "referer": "https://www.pixiv.net/",
    }
    try:
        response = requests.get(url, headers=headers, proxies=proxies)
        if response.status_code == 200 and response.content:
            with open(path, "wb") as file:
                file.write(response.content)
            return os.path.exists(path) and os.path.getsize(path) > 0
        return False
    except Exception as e:
        print(f"Download failed: {e}")
        return False

# 提取文件名
def extract_filename(url):
    try:
        return urlparse(url).path.split("/")[-1]
    except:
        print(f"Error parsing URL: {url}")
        return None

# 下载插画
def download_illustrations():
    def download_page(pid, page_index, page):
        key = f"{pid}_{page_index}"
        if key not in history:
            print(f"Downloading {key}.jpg", end=" ")
            filename = extract_filename(page["urls"]["original"])
            if filename and download_image(
                page["urls"]["original"], f"pixiv/{filename}"
            ):
                history[key] = True
                with open("history.json", "w") as f:
                    json.dump(history, f, indent=4)
                print("...ok")
            else:
                print("...fail")
        else:
            pass  # Already downloaded

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        futures = []
        for pid in bookmarks:
            if "picture" in bookmarks[pid] and "body" in bookmarks[pid]["picture"]:
                pages = bookmarks[pid]["picture"]["body"]
                for page_index, page in enumerate(pages):
                    futures.append(
                        executor.submit(download_page, pid, page_index, page)
                    )

        for future in concurrent.futures.as_completed(futures):
            try:
                future.result()
            except Exception as e:
                print(f"Download error: {e}")

# 主程序
if __name__ == "__main__":
    if not check_login_status():
        print("Login failed")
    else:
        print("Login successful")
        update_bookmarks()
        fetch_illustration_data()
        download_illustrations()

免费评分

参与人数 19吾爱币 +21 热心值 +16 收起 理由
kbsbb + 1 + 1 感谢大佬分享
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Alfilite + 1 谢谢@Thanks!
yaoyici + 1 + 1 大佬牛!
weidagee + 1 热心回复!
zade233 + 1 + 1 谢谢@Thanks!
tms111222 + 1 + 1 我很赞同!太妙了
zhrwrqk + 1 + 1 我很赞同!
Writeww + 1 + 1 我很赞同!
Talina + 1 谢谢@Thanks!
Renlik + 1 + 1 谢谢@Thanks!
a105116 + 1 我很赞同!
alienqueen + 1 我很赞同!
feiyingyishan + 1 + 1 很有用!
626202696 + 1 + 1 热心回复!
xyz0225 + 1 + 1 热心回复!
moldr34 + 1 感谢大佬分享
A0f212 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Gezp + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
Guiji888 发表于 2025-3-13 11:50
感谢大佬分享,收藏备用
推荐
lyyzs7742 发表于 2025-3-14 17:52
太感谢了,我以前要一个一个点击进行下载  人肉脚本太消耗时间了 不过筛选的质量还是挺好的 都是自己爱看的  同一个作者的风格几乎一样lsp喜欢的点也是一样的(笑
沙发
227883368 发表于 2025-3-13 11:28
3#
NaCl666 发表于 2025-3-13 11:48

感谢大佬分享
5#
a971155 发表于 2025-3-13 11:55
下载 用吗
6#
想不出名字 发表于 2025-3-13 12:09
感谢大佬分享
7#
加奈绘 发表于 2025-3-13 12:16
感谢分享,这下可以一口气备份收藏的画了
8#
JW5120 发表于 2025-3-13 12:32
收藏的插画需要购买么
9#
说书人i 发表于 2025-3-13 13:17
感谢大佬分享
10#
yiran99 发表于 2025-3-13 14:37
感谢分享!平时都只能看看,截图又不清晰
现在好了,这就去找漂亮图片
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-25 05:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表