Instagram scraping login

Viewed 479

I am scraping comments from multiple Instagram posts, with this code below. At first, I didn't need to log in to my account, because The news was already open and all I had to do is close one pop up then scrape. But after a few runs, the browser in selenium asks me to log in, so I had to add a login operation on my code below. I have successfully logged in using my code below, but instead of opening the post that I want to scrape, it opens my timeline. How can I open the post that I want to scrape after login?

from selenium.webdriver.common.by import By
from selenium import webdriver
import time
import sys
import pandas as pd
from pandas import ExcelWriter
import os.path
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains



url=['https://www.instagram.com/p/CRLe53_hmMH','https://www.instagram.com/p/CRX7VL1sL54/?utm_medium=share_sheet',
     'https://www.instagram.com/p/CRVB7ykM7-R/?utm_medium=share_sheet', 'https://www.instagram.com/p/CRQ9Bq5M6ce/?utm_medium=share_sheet',
     'https://www.instagram.com/p/CRQT1BJMmSi/?utm_medium=share_sheet', 'https://www.instagram.com/p/CM8T3HgMQG0/?utm_medium=copy_link'
     'https://www.instagram.com/p/COrn5fYs78O/?utm_medium=share_sheet']
user_names = []
user_comments = []

for n in url:
    driver = driver = webdriver.Chrome('E:/chromedriver')
    driver.get(n)
    wait = WebDriverWait(driver, 10)


    time.sleep(3)
    
    #if user not logined
    try:
        username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
        password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))
        username.clear()
        username.send_keys('my_username')
        password.clear()
        password.send_keys('my_password')
        Login_button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
        
        time.sleep(4)
        not_now = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]'))).click()
        not_now2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]'))).click()
        not_now.click()
        not_now2.click()
        
        
        try:
            
            load_more_comment = driver.find_element_by_css_selector('.MGdpg > button:nth-child(1)')
            print("Found {}".format(str(load_more_comment)))
            i = 0
            while load_more_comment.is_displayed() and i < 10:
                load_more_comment.click()
                time.sleep(1.5)
                load_more_comment = driver.find_element_by_css_selector('.MGdpg > button:nth-child(1)')
                print("Found {}".format(str(load_more_comment)))
                i += 1
        except Exception as e:
            print(e)
            pass
    except:
        pass
    
    

    

    comment = driver.find_elements_by_class_name('gElp9 ')
    for c in comment:
        container = c.find_element_by_class_name('C4VMK')
        name = container.find_element_by_class_name('_6lAjh').text
        content = container.find_element_by_tag_name('span').text
        content = content.replace('\n', ' ').strip().rstrip()
        user_names.append(name)
        user_comments.append(content)
        #print(content)
    
    
    user_names.pop(0)
    user_comments.pop(0)
    
    
    #export(user_names, user_comments)
    
    driver.close()
df = pd.DataFrame(list(zip(user_names, user_comments)),
               columns =['Name', 'Comments'])
df.to_excel('ujicoba_gabung_IG_4.xlsx')
print(df)
1 Answers

You see login prompt from Instagram because you are being blocked. Instagram detects that you are not manually browsing their website.

If you want to extract info from Instagram page you have to rely on an API for scraping since Instagram will block you very quickly.

Here is a good tutorial on scraping user profile data and posts that handles pagination using an API for scraping: https://scrapingfish.com/blog/scraping-instagram

Related