APIなしでTiktokやInstagramのフォロワー数を取得する

スポンサーリンク
スポンサーリンク

APIなしでTiktokやInstagramのフォロワー数を取得する

Tiktok

Instagram

YouTube

X ※今後対応

これら各種SNSのフォロワー数をPythonで取得します

実装

import requests
import re

# seleniumを使用する場合
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import re
import time

# 数値 → K / M 表記に変換
def format_number(n):
    if n >= 1_000_000:
        return f"{n / 1_000_000:.1f}M"
    elif n >= 1_000:
        return f"{n / 1_000:.1f}K"
    else:
        return str(n)

# 初期値
tiktokFllowers = 73800
youtubeFllowers = 252000
instagramFllowers = 26000
xFllowers = 66

# tiktok
# ヘッドレスブラウザの設定(画面表示なし)
options = Options()
options.add_argument("--headless")
# Chromeドライバを起動(ドライバは事前にインストールしてパスを通す)
driver = webdriver.Chrome(options=options)
# 取得したいURLにアクセス
driver.get("https://www.tiktok.com/@kinarimovie?lang=ja-JP")
# JavaScript実行待ち(必要なら適宜変更)
time.sleep(5)
# ページ全体のHTML取得
tiktokHtmlContent = driver.page_source
# フォロワー抽出(先ほどのロジック)
tiktokMatch = re.search(r'"desc":".*?(\d+(?:\.\d+)?)([kKmM])\s*フォロワー', tiktokHtmlContent)
if tiktokMatch:
    tiktokNumber = float(tiktokMatch.group(1))
    tiktokSuffix = tiktokMatch.group(2).lower()
    # 単位を変換
    if tiktokSuffix == 'k':
        tiktokFllowers = int(tiktokNumber * 1_000)
    elif tiktokSuffix == 'm':
        tiktokFllowers = int(tiktokNumber * 1_000_000)
    else:
        tiktokFllowers = int(tiktokNumber)
    print(f"tiktokFllowers:{tiktokFllowers:,}")
else:
    print("tiktokFllowers not found.")
driver.quit()

# youtube
youtubeUrl = "https://www.youtube.com/@kinarimovie"
youtubeResponse = requests.get(youtubeUrl)
youtubeHtmlContent = youtubeResponse.text
youtubeMatch = re.search(r'(\d+(?:\.\d+)?)万人', youtubeHtmlContent)
if youtubeMatch:
    youtubeNumber = float(youtubeMatch.group(1))
    youtubeFllowers = int(youtubeNumber * 10_000)
    print(f"youtubeFllowers:{youtubeFllowers:,}") # カンマ付きで出力
else:
    print("youtubeFllowers not found.")

# instagram
instagramUrl = "https://www.instagram.com/kinarimovie/"
instagramResponse = requests.get(instagramUrl)
instagramHtmlContent = instagramResponse.text
instagramMatch = re.search(r'(\d+(?:\.\d+)?)([KM]?) Followers', instagramHtmlContent)
if instagramMatch:
    #instagramFllowers = instagramMatch.group(1) # 26Kのみの場合
    instagramNumber = float(instagramMatch.group(1))
    suffix = instagramMatch.group(2)
    # 単位を変換
    if suffix == 'K':
        instagramFllowers = int(instagramNumber * 1_000)
    elif suffix == 'M':
        instagramFllowers = int(instagramNumber * 1_000_000)
    else:
        instagramFllowers = int(instagramNumber)
    # instagramFllowers = instagramMatch.group(1) # 26Kのみの場合
    print(f"instagramFllowers:{instagramFllowers:,}") # カンマ付きで出力
else:
    print("instagramFllowers not found.")

# x
#xUrl = "https://www.instagram.com/kinarimovie/"
#xResponse = requests.get(xUrl)
#xHtmlContent = xResponse.text
#xMatch = re.search(r'(\d+[KM]?) Followers', xHtmlContent)
#if xMatch:
#    xFllowers = xMatch.group(1)
#    print(f"xFllowers:{xFllowers}")
#else:
#    print("xFllowers not found.")
print(f"xFllowers:{xFllowers}")

# 合計
totalFllowers = tiktokFllowers + youtubeFllowers + instagramFllowers + xFllowers
#print(f"totalFllowers:{totalFllowers:,}") # カンマ付きで出力
totalFllowers = format_number(totalFllowers)
print(f"totalFllowers:{totalFllowers}")

説明

実装は2種類あります。

①ページのソースを取得

基本的にはページソースを取得してフォロワー数の部分だけを抽出しています。

Instagram、YouTubeはこちらで実装可能です。

②selenium

動的Webサイトではページソースを取得してもコンテンツが表示されません。

そのため、seleniumを使用して実現します。

使用する際はインストールが必要です。

pip install selenium

①の応用でYouTubeの総視聴回数も取得できます。

# youtube
youtubeAboutUrl = "https://www.youtube.com/@kinarimovie/about"
youtubeResponse = requests.get(youtubeAboutUrl)
youtubeHtmlContent = youtubeResponse.text
# カンマ付きの数字を抽出
match = re.search(r'"viewCountText":"([\d,]+)\s*回視聴"', youtubeHtmlContent)
if match:
    youtubeViewCountStr = match.group(1).replace(",", "")
    youtubeViewCount = int(youtubeViewCountStr)
    youtubeViewCount = format_number(youtubeViewCount)
    print(f"youtubeViewCount: {youtubeViewCount}")
else:
    print("youtubeViewCount not found.")

コメント

タイトルとURLをコピーしました