2016-05-15 16 views
2

誰でも、特定のユーザーのInstagramフォロワーを表示するために基礎となるURLにアクセスする方法を教えてもらえますか?私はInstagram APIでこれを行うことができますが、承認プロセスの変更が保留されているので、私はスクレイピングに切り替えることにしました。Instagram Webブラウザからフォロワーをスクレープする方法は?

Instagram Webブラウザを使用すると、Instagramのフォロワーを表示するなどの任意の公開ユーザーのフォロワーリストを表示し、「https://www.instagram.com/instagram」にアクセスし、フォロワーURLをクリックして、視聴者にページを変更するウィンドウを開くことができます注:これを表示するには、アカウントにログインしている必要があります)。

このウィンドウがポップアップするとURLが "https://www.instagram.com/instagram/followers"に変わりますが、このURLの基になるページソースを表示できないようです。

ブラウザウィンドウに表示されるので、私は掻きすることができると思います。しかし、私はセレンのようなパッケージを使用する必要がありますか?基本URLが何であるか知っている人は誰ですか?私はSeleniumを使用する必要はありませんか?

例として、「instagram.com/instagram/media/」にアクセスして、基礎となるフィードデータに直接アクセスすることができます。このデータから、すべての繰り返しをスクラップして改ページできます。フォロワーのリストと似たようなことをしたいと思い、このデータに直接アクセスしてください(セレンを使うよりも)。

+0

フォロワーリストをスクラップすることはできますが、Instagramにログオンする必要があります。プライベートユーザーのフォロワーは取得できません。 –

答えて

7

あなたの質問はちょっと混乱しています。たとえば、「すべての反復で掻き集めてページをめくることができる」ということは、実際にはどういう意味なのかは分かりません。あなたは現在、掻きとページ付けに何を使用していますか?

instagram.com/instagram/media/は、instagram.com/instagram/followersと同じタイプのエンドポイントではありません。 mediaエンドポイントはREST APIのように見え、簡単に解析できるJSONオブジェクトを返すように設定されています。

followersエンドポイントは実際に私が知ることのできるRESTfulエンドポイントではありません。むしろ、Followersボタンをクリックした後、Instagram AJAXはページソースへの情報に(React?を使用して)表示されます。私はあなたがユーザーにフォロワーを表示するjavascriptをロード/レンダリングすることができるSeleniumのようなものを使用せずにその情報を得ることができるとは思わない。

この例のコードは動作します:

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 


def login(driver): 
    username = "" # <username here> 
    password = "" # <password here> 

    # Load page 
    driver.get("https://www.instagram.com/accounts/login/") 

    # Login 
    driver.find_element_by_xpath("//div/input[@name='username']").send_keys(username) 
    driver.find_element_by_xpath("//div/input[@name='password']").send_keys(password) 
    driver.find_element_by_xpath("//span/button").click() 

    # Wait for the login page to load 
    WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.LINK_TEXT, "See All"))) 


def scrape_followers(driver, account): 
    # Load account page 
    driver.get("https://www.instagram.com/{0}/".format(account)) 

    # Click the 'Follower(s)' link 
    driver.find_element_by_partial_link_text("follower").click() 

    # Wait for the followers modal to load 
    xpath = "//div[@style='position: relative; z-index: 1;']/div/div[2]/div/div[1]" 
    WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.XPATH, xpath))) 

    # You'll need to figure out some scrolling magic here. Something that can 
    # scroll to the bottom of the followers modal, and know when its reached 
    # the bottom. This is pretty impractical for people with a lot of followers 

    # Finally, scrape the followers 
    xpath = "//div[@style='position: relative; z-index: 1;']//ul/li/div/div/div/div/a" 
    followers_elems = driver.find_elements_by_xpath(xpath) 

    return [e.text for e in followers_elems] 


if __name__ == "__main__": 
    driver = webdriver.Chrome() 
    try: 
     login(driver) 
     followers = scrape_followers(driver, "instagram") 
     print(followers) 
    finally: 
     driver.quit() 

をこのアプローチは、それらの間のチーフは、それは、APIに関連しているか、低速であること、多くの理由から問題があります。

+0

すばらしい、時間をとっていただきありがとうございます。私はちょっと混乱しているので、私の質問はおそらく混乱していました!私は貿易によってプログラマーではなく、物事のバックエンドを本当に理解していません。しかし、あなたの反応はとても役に立ち、私が探していたものです! – user812783765

+0

@ user812783765これがあなたが探していた答えであれば、この回答を受け入れてください。 – sowa

+0

これはかなり巧妙なスクリプトです。 PHPの実装で、探索ハッシュタグのページ上でもっと多くのボタンをクリックすることができます。 – socca1157

1

以前の回答は機能しなくなったので、スクロール機能を含む以前の回答に基づいて更新されたバージョンを作成しました(最初に読み込まれたものだけでなく、リスト内のすべてのユーザーを取得します)。さらに、これは追従者を追いかけるとともに、追従する。 (download chromedriverも必要です)

import time 
from selenium import webdriver as wd 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

# The account you want to check 
account = "" 

# Chrome executable 
chrome_binary = r"chrome.exe" # Add your path here 


def login(driver): 
    username = "" # Your username 
    password = "" # Your password 

    # Load page 
    driver.get("https://www.instagram.com/accounts/login/") 

    # Login 
    driver.find_element_by_xpath("//div/input[@name='username']").send_keys(username) 
    driver.find_element_by_xpath("//div/input[@name='password']").send_keys(password) 
    driver.find_element_by_xpath("//span/button").click() 

    # Wait for the login page to load 
    WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.LINK_TEXT, "See All"))) 


def scrape_followers(driver, account): 
    # Load account page 
    driver.get("https://www.instagram.com/{0}/".format(account)) 

    # Click the 'Follower(s)' link 
    driver.find_element_by_partial_link_text("follower").click() 

    # Wait for the followers modal to load 
    xpath = "/html/body/div[4]/div/div/div[2]/div/div[2]" 
    WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.XPATH, xpath))) 

    SCROLL_PAUSE = 0.5 # Pause to allow loading of content 
    driver.execute_script("followersbox = document.getElementsByClassName('_gs38e')[0];") 
    last_height = driver.execute_script("return followersbox.scrollHeight;") 

    # We need to scroll the followers modal to ensure that all followers are loaded 
    while True: 
     driver.execute_script("followersbox.scrollTo(0, followersbox.scrollHeight);") 

     # Wait for page to load 
     time.sleep(SCROLL_PAUSE) 

     # Calculate new scrollHeight and compare with the previous 
     new_height = driver.execute_script("return followersbox.scrollHeight;") 
     if new_height == last_height: 
      break 
     last_height = new_height 

    # Finally, scrape the followers 
    xpath = "/html/body/div[4]/div/div/div[2]/div/div[2]/ul/li" 
    followers_elems = driver.find_elements_by_xpath(xpath) 

    followers_temp = [e.text for e in followers_elems] # List of followers (username, full name, follow text) 
    followers = [] # List of followers (usernames only) 

    # Go through each entry in the list, append the username to the followers list 
    for i in followers_temp: 
     username, sep, name = i.partition('\n') 
     followers.append(username) 

    print("______________________________________") 
    print("FOLLOWERS") 

    return followers 

def scrape_following(driver, account): 
    # Load account page 
    driver.get("https://www.instagram.com/{0}/".format(account)) 

    # Click the 'Following' link 
    driver.find_element_by_partial_link_text("following").click() 

    # Wait for the following modal to load 
    xpath = "/html/body/div[4]/div/div/div[2]/div/div[2]" 
    WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.XPATH, xpath))) 

    SCROLL_PAUSE = 0.5 # Pause to allow loading of content 
    driver.execute_script("followingbox = document.getElementsByClassName('_gs38e')[0];") 
    last_height = driver.execute_script("return followingbox.scrollHeight;") 

    # We need to scroll the following modal to ensure that all following are loaded 
    while True: 
     driver.execute_script("followingbox.scrollTo(0, followingbox.scrollHeight);") 

     # Wait for page to load 
     time.sleep(SCROLL_PAUSE) 

     # Calculate new scrollHeight and compare with the previous 
     new_height = driver.execute_script("return followingbox.scrollHeight;") 
     if new_height == last_height: 
      break 
     last_height = new_height 

    # Finally, scrape the following 
    xpath = "/html/body/div[4]/div/div/div[2]/div/div[2]/ul/li" 
    following_elems = driver.find_elements_by_xpath(xpath) 

    following_temp = [e.text for e in following_elems] # List of following (username, full name, follow text) 
    following = [] # List of following (usernames only) 

    # Go through each entry in the list, append the username to the following list 
    for i in following_temp: 
     username, sep, name = i.partition('\n') 
     following.append(username) 

    print("\n______________________________________") 
    print("FOLLOWING") 
    return following 


if __name__ == "__main__": 
    options = wd.ChromeOptions() 
    options.binary_location = chrome_binary # chrome.exe 
    driver_binary = r"chromedriver.exe" 
    driver = wd.Chrome(driver_binary, chrome_options=options) 
    try: 
     login(driver) 
     followers = scrape_followers(driver, account) 
     print(followers) 
     following = scrape_following(driver, account) 
     print(following) 
    finally: 
     driver.quit() 
関連する問題