Pythonを使用してInstagramからデータを取得する方法
この便利なライブラリを使用すると、Instagram からのデータの取得が簡単になります。
Instagram は、数十億人のユーザーを抱える最も人気のあるソーシャル メディア サイトの 1 つです。学生から芸能人まで、誰もがインスタグラムのアカウントを持っています。 Instagram の公開データは、企業、マーケティング担当者、個人にとって非常に価値があります。誰でもこのデータを使用して、データ分析を実行し、マーケティングをターゲットにし、洞察を生成できます。
Python を使用して、Instagram データを抽出する自動ツールを構築できます。
必要なライブラリのインストール
Instaloader は、Instagram から公開されているデータを抽出するために使用できる Python ライブラリです。画像、ビデオ、ユーザー名、番号などのデータにアクセスできます。 Instaloaderを使用して、投稿数、フォロワー数、フォロー数、プロフィールなどを確認します。 Instaloader は、いかなる形でも Instagram と提携、認可、維持、または承認されていないことに注意してください。
pip 経由で instaloader をインストールするには、次のコマンドを実行します。
pip install instaloader
外部 Python ライブラリをインストールするには、システムに pip がインストールされている必要があります。
次に、Pandas Python ライブラリをインストールする必要があります。 Pandas は、主にデータ操作とデータ分析を実行するために使用される Python ライブラリです。次のコマンドを実行してインストールします。
pip install pandas
これで、コードのセットアップと Instagram からのデータの取得を開始する準備が整いました。
コードのセットアップ
Instagram データ取得ツールを設定するには、Instaloader Python ライブラリをインポートし、Instaloader クラスのインスタンスを作成する必要があります。その後、データを抽出するプロフィールの Instagram ハンドルを指定する必要があります。
Instagram Extractor Python コードは GitHub リポジトリで入手でき、MIT ライセンスに基づいて無料で使用できます。
import instaloader
# Creating an instance of the Instaloader class
bot = instaloader.Instaloader()
# Loading the profile from an Instagram handle
profile = instaloader.Profile.from_username(bot.context, 'cristiano')
print(profile)
これは、基本的な動作を確認するための良い最初のステップです。エラーのない意味のあるデータが表示されるはずです。
プロファイルからのデータの抽出
ユーザー名、番号など、公開されている貴重なデータを抽出できます。わずか数行のコードで Instaloader を使用して、投稿数、フォロワー数、フォロー数、プロフィール、ユーザー ID、外部 URL を取得できます。プロフィールの Instagram ハンドルを指定するだけです。
import instaloader
import pandas as pd
# Creating an instance of the Instaloader class
bot = instaloader.Instaloader()
# Loading a profile from an Instagram handle
profile = instaloader.Profile.from_username(bot.context, 'leomessi')
print("Username: ", profile.username)
print("User ID: ", profile.userid)
print("Number of Posts: ", profile.mediacount)
print("Followers Count: ", profile.followers)
print("Following Count: ", profile.followees)
print("Bio: ", profile.biography)
print("External URL: ", profile.external_url)
指定したハンドルから多くのプロファイル情報が表示されるはずです。
プロフィールから電子メールを抽出する
正規表現を使用して、任意のプロフィールのインスタのプロフィールから電子メール アドレスを抽出できます。 Python の re ライブラリをインポートし、電子メールを検証するための正規表現をパラメータとして re.findall() メソッドに渡す必要があります。
import instaloader
import re
# Creating an instance of Instaloader class
bot = instaloader.Instaloader()
profile = instaloader.Profile.from_username(bot.context, "wealth")
print("Username: ", profile.username)
print("Bio: ", profile.biography)
emails = re.findall(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", profile.biography)
print("Emails extracted from the bio:")
print(emails)
スクリプトは、電子メール アドレスとして認識したものをすべてプロフィールに出力します。
上位の検索結果データの抽出
Instagram で何かを検索すると、ユーザー名やハッシュタグを含むいくつかの結果が表示されます。 get_profiles() メソッドと get_hashtags() メソッドを使用して、上位の検索結果を抽出できます。 instaloader.TopSearchResults() メソッドで検索クエリを指定するだけです。さらに、個々の結果を反復して印刷/保存することができます。
import instaloader
# Creating an instance of the Instaloader class
bot = instaloader.Instaloader()
# Provide the search query here
search_results = instaloader.TopSearchResults(bot.context, 'music')
# Iterating over the extracted usernames
for username in search_results.get_profiles():
print(username)
# Iterating over the extracted hashtags
for hashtag in search_results.get_hashtags():
print(hashtag)
出力には、一致するユーザー名とハッシュタグが含まれます。
アカウントのフォロワーとフォローを抽出する
Instaloader を使用して、アカウントのフォロワーとそのアカウント自体がフォローしているフォロワーを抽出できます。このデータを取得するには、Instagram のユーザー名とパスワードを入力する必要があります。
Instagram からデータを抽出するために個人アカウントを使用しないでください。アカウントが一時的または永久に禁止される可能性があります。
Instaloader クラスのインスタンスを作成した後、ユーザー名とパスワードを入力する必要があります。これは、ボットがあなたのアカウントを使用して Instagram にログインし、フォロワーとフォローのデータを取得できるようにするためです。
次に、ターゲットプロファイルの Instagram ハンドルを指定する必要があります。 get_followers() メソッドと get_followees() メソッドは、フォロワーとフォロー対象者を抽出します。フォロワーとフォロー対象者のユーザー名は、それぞれ follower.username プロパティと followee.username プロパティを使用して取得できます。
結果を CSV ファイルに保存する場合は、まずデータを Pandas DataFrame オブジェクトに変換する必要があります。 pd.DataFrame() メソッドを使用して、リスト オブジェクトを DataFrame に変換します。
最後に、to_csv() メソッドを使用して DataFrame オブジェクトを CSV ファイルにエクスポートできます。 CSV ファイル形式でエクスポートされたデータを取得するには、filename.csv をパラメータとしてこのメソッドに渡す必要があります。
アカウント所有者のみがすべてのフォロワーとフォローを表示できます。この方法や他の方法を使用しても、すべてのフォロワーとフォローしているデータを抽出することはできません。
# Importing Libraries
import instaloader
import pandas as pd
# Creating an instance of the Instaloader class
bot = instaloader.Instaloader()
bot.login(user="Your_username", passwd="Your_password")
# Loading a profile from an Instagram handle
profile = instaloader.Profile.from_username(bot.context, 'Your_target_account_insta_handle')
# Retrieving the usernames of all followers
followers = [follower.username for follower in profile.get_followers()]
# Converting the data to a DataFrame
followers_df = pd.DataFrame(followers)
# Storing the results in a CSV file
followers_df.to_csv('followers.csv', index=False)
# Retrieving the usernames of all followings
followings = [followee.username for followee in profile.get_followees()]
# Converting the data to a DataFrame
followings_df = pd.DataFrame(followings)
# Storing the results in a CSV file
followings_df.to_csv('followings.csv', index=False)
Instagram アカウントから投稿をダウンロードする
繰り返しになりますが、任意のアカウントから投稿をダウンロードするには、ユーザー名とパスワードを入力する必要があります。これは、ボットがあなたのアカウントを使用して Instagram にログインできるようにするためです。 get_posts() メソッドを使用して、すべての投稿のデータを取得できます。また、download_post() メソッドを使用して、すべての個々の投稿を反復してダウンロードできます。
# Importing Libraries
import instaloader
import pandas as pd
# Create an instance of Instaloader class
bot = instaloader.Instaloader()
bot.login(user="Your_username",passwd="Your_password")
# Loading a profile from an Instagram handle
profile = instaloader.Profile.from_username(bot.context, 'Your_target_account_insta_handle')
# Retrieving all posts in an object
posts = profile.get_posts()
# Iterating and downloading all the individual posts
for index, post in enumerate(posts, 1):
bot.download_post(post, target=f"{profile.username}_{index}")
Python を使用して Web をスクレイピングする
データ スクレイピングまたは Web スクレイピングは、Web から有用な情報を抽出する最も一般的な方法の 1 つです。抽出したデータは、マーケティング、コンテンツ作成、意思決定に使用できます。
Python はデータ スクレイピングに推奨される言語です。 BeautifulSoup、Scrapy、Pandas などのライブラリにより、データの抽出、分析、視覚化が簡素化されます。