LoginSignup
0
0

雀魂のスタンプをスクレイビングする

Last updated at Posted at 2024-02-21

はじめに

雀魂のスタンプって汎用性高いですよね。
かわいい系シュール系なんでもござれで対局中以外にも使いたいなと思ってしまいます。

そこで、Pythonのスクレイビングを使って雀魂DBからスタンプ画像を収集しました。
なお、収集したスタンプは研究室slackチャンネルのカスタム絵文字にしれっと入れました(1500個ほど)

必要なもの

Pythonの実行環境
bs4などのライブラリはpip installで適宜インストールしてください

※スクレイビングについてはこちらの記事"Pythonで手軽に始めるWebスクレイピング"が分かりやすいです。

サンプルコード

ChatGPTにやってもらいました。

  • 雀魂DBのスタンプ一覧からページ番号を指定すると、そのページのスタンプ画像を1秒おきにダウンロードします。(負荷をかけないペース)
  • サムネイルだと画質がよくないので、画像を開いた後のurlを引っ張っています。
  • 現在14ページまでスタンプがあるので、まとめてダウンロードしたい方はコメントアウトしてあるfor文を回してください。
bs4.py
# 1-14の数字nを入力すると,雀魂データベースのスタンプ一覧の
# nページにあるスタンプのpng画像を同じ階層フォルダにまとめてダウンロードします
# ダウンロードの間隔は1秒に設定しています.

import requests
from bs4 import BeautifulSoup
import os
import urllib.request
import time

N = int(input('page number: '))

# for N in range(1,15):
#     time.sleep(30.0)

# ウェブサイトのURL
if N==1:
    url = 'https://mahjongsoul.club/characters/emotes'
else:
    url = 'https://mahjongsoul.club/characters/emotes?sort_by=field_character_er_target_id&sort_order=ASC&page='+str(N-1)

# ウェブサイトからHTMLを取得
response = requests.get(url)
response.raise_for_status()  # ステータスコードが200でない場合は、エラーを発生させる

# BeautifulSoupオブジェクトを作成し、HTMLを解析
soup = BeautifulSoup(response.text, 'html.parser')

# divタグのclassがview-contentのものを探し、その中のimgタグを全て取得
images = soup.find('div', class_='view-content').find_all('img')

# urlをサムネから置き換える
image_links = []
for img in images:
    link = img['src']
    link = link.replace('/styles/thumbnail/public','') 
    link = link.split('?')[0]
    image_links.append(link)

# ダウンロードする画像を保存するフォルダを指定(存在しない場合は作成)
download_folder = 'downloaded_images'
if not os.path.exists(download_folder):
    os.makedirs(download_folder)


# 各画像URLから画像をダウンロードして保存
for link in image_links:
    # 画像データを取得
    file_name = 'jantama-' + link.split('/')[-1]
    file_path = os.path.join(download_folder, file_name)
    print(file_name, file_path)
    urllib.request.urlretrieve(link, file_path)
    time.sleep(1.0)
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0