A Simple Rule34 Post Downloader
This tutorial walks you through creating a simple script that searches for and downloads a selection of posts from the rule34.xxx website using the rule34Py module.
Install python dependencies from PyPI, using PIP (or your python package manager of choice).
python -m pip install rule34Py
python -m pip install requests
Import the
rule34Py
module and instantiate arule34Py
client object.
The client brokers interactions with the public Rule34.xxx API endpoints. It will transparently use the correct endpoint for whatever operation you request. Data returned by the API will be marshalled into native python data types.
1from rule34Py import rule34Py
2
3client = rule34Py()
Use the
rule34Py.search()
method to search your tags.
The search()
method accepts a list of tags.
You can use the same tag syntaxes that are supported on the interactive site’s Searching Cheatsheet.
5TAGS = ["neko", "sort:score", "-video"]
6
7results = client.search(tags=TAGS)
Important
In this example, we are excluding posts tagged “video”, so that we do not have to handle them specially during the download step.
Note
By default, the search()
method will return the first 1000 search results.
You can change this behavior by setting the limit
method parameter. For this example, we will only download the first 3 results, and ignore the remainder.
Download the images and save them to your local disk.
9from pathlib import Path
10import requests
11
12DOWNLOAD_LIMIT = 3
13
14for result in results[0:DOWNLOAD_LIMIT]:
15 print(f"Downloading post {result.id} ({result.image}).")
16 with open(Path(result.image).name, "wb") as fp_output:
17 resp = requests.get(result.image)
18 resp.raise_for_status()
19 fp_output.write(resp.content)
Example Script
The complete, example script might look like this.
1from rule34Py import rule34Py
2
3client = rule34Py()
4
5TAGS = ["neko", "sort:score", "-video"]
6
7results = client.search(tags=TAGS)
8
9from pathlib import Path
10import requests
11
12DOWNLOAD_LIMIT = 3
13
14for result in results[0:DOWNLOAD_LIMIT]:
15 print(f"Downloading post {result.id} ({result.image}).")
16 with open(Path(result.image).name, "wb") as fp_output:
17 resp = requests.get(result.image)
18 resp.raise_for_status()
19 fp_output.write(resp.content)