Searching for pixiv tags and saving illustrations using Python

Introduction

I wanted to see a lot of pixiv illustrations from Python. There seems to be little Japanese information on pixivpy, so I will leave it as a memo.

environment

Windows10 Python 3.8 pixivpy 3.5.8 : https://github.com/upbit/pixivpy

Preparation

Prepare pixivpy From the command prompt

pip install pixivpy

Run

code

tagsearch_pixivpy.py


from pixivpy3 import PixivAPI
from pixivpy3 import AppPixivAPI

import json
import os
from time import sleep

def login(id, password):
    api = PixivAPI()
    api.login(id, password)
    return api

def search_and_save(apilogin, searchtag, min_score, range_num, directory):
    api = apilogin
    aapi = AppPixivAPI()

    saving_dir_path = os.path.join(directory, searchtag)
    if not os.path.exists(saving_dir_path):
        os.mkdir(saving_dir_path)

    for page in range(1, range_num + 1):
        json_result = api.search_works(searchtag, page=page, mode='tag')
        illust_len = len(json_result.response)

        for i in range(0, illust_len):
            illust = json_result.response[i]
            score = illust.stats.score

            if score <= min_score:
                continue
            else:
                print("Cartoon:" + str(illust.page_count) + "page") if illust.is_manga else print("An illustration")
                if illust.is_manga:
                    print(">>> title:", illust.title)
                    manga_info = api.works(illust.id)
                    for page_no in range(0, manga_info.response[0].page_count):
                        page_info = manga_info.response[0].metadata.pages[page_no]
                        aapi.download(page_info.image_urls.large, path=saving_dir_path)
                        sleep(1)
                else:
                    print(">>> title:", illust.title)
                    aapi.download(illust.image_urls.large, path=saving_dir_path)
                    sleep(1)

def main():
    searchtag = "Search tag"  #Search tagを入力。半角スペースで分けることで複数タグ検索可能
    min_score = 2000    #DL only for illustrations above this score
    range_num = 1   #Search up to the page with this value. 30 sheets per 1p
    directory = 'Destination directory'  #Create a folder with the search tag name under the specified directory and save it there.

    apilogin  = login("User name", "password") #User nameとpassword入力

    search_and_save(apilogin, searchtag, min_score, range_num, directory)

if __name__ == '__main__':
	main()

You can search as you like by changing the variables below the main function. It also supports posting in manga format (a format in which one title has multiple illustrations).

idea

--I want to save directly to Google Drive --Pick up the top 20% of the searched illustrations in order of score

Reference site

-Collect an infinite number of illustrations from pixiv [python] -I wanted to get an infinite number of illustrations from any illustrator from pixiv -Batch DL of pixiv follower's comics and illustrations

All of them were very helpful in their polite articles.

Recommended Posts

Searching for pixiv tags and saving illustrations using Python
Initial settings for using Python3.8 and pip on CentOS8
Extendable skeletons for Vim using Python, Click and Jinja2
Read and write NFC tags in python using PaSoRi
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
This and that for using Step Functions with CDK + Python
Authentication using tweepy-User authentication and application authentication (Python)
[TouchDesigner] Tips for for statements using python
Clustering and visualization using Python and CytoScape
[Python] Reasons for overriding using super ()
[Python] Multiplication table using for statement
Dump, restore and query search for Python class instances using mongodb
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.
From Python to using MeCab (and CaboCha)
Python development environment for macOS using venv 2016
[50 counts] Key transmission using Python for Windows
Using Python and MeCab with Azure Databricks
[python, multiprocessing] Behavior for exceptions when using multiprocessing
6 Python libraries for faster development and debugging
Tips for using python + caffe with TSUBAME
Implementation and explanation using XGBoost for beginners
Notes for using python (pydev) in eclipse
Scraping using lxml and saving to MySQL
I'm using tox and Python 3.3 with Travis-CI
SublimeText2 and SublimeLinter --Syntax check for Python3--
Effective Python Note Item 12 Avoid using else blocks after for and while loops
Head orientation estimation using Python and OpenCV + dlib
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
Causal reasoning and causal search with Python (for beginners)
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Notes on installing Python3 and using pip on Windows7
Develop and deploy Python APIs using Kubernetes and Docker
Python development flow using Poetry, Git and Docker
I tried object detection using Python and OpenCV
Python pandas: Search for DataFrame using regular expressions
Get note information using Evernote SDK for Python 3
Create a web map using Python and GDAL
Refined search for Pokemon race values using Python
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
[Python / Chrome] Basic settings and operations for scraping
[Python3] Automatic sentence generation using janome and markovify
PDF files and sites useful for learning Python 3
Try using tensorflow ① Build python environment and introduce tensorflow
Create a Mac app using py2app and Python3! !!
Let's make a module for Python using SWIG
Try using ChatWork API and Qiita API in Python
Install Python and libraries for Python on MacOS Catalina
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby