Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Need help scraping Instagram reels statistics using Selenium

$
0
0

I'm trying to scrape statistics from Instagram reels using Selenium in Python. However, I encountered a warning message from Instagram, which states:

This is a browser feature intended for developers. If someone told you to copy-paste something here to enable an Instagram feature or 'hack' someone's account, it is a scam and will give them access to your Instagram account.

I'm using the following libraries:

SeleniumWebDriver for ChromeHere's the code I've written:

from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC# Function to obtain statistics of an Instagram reeldef get_reel_stats(url):    try:        # Browser configuration        options = Options()        options.add_argument('--headless')  # To run Chrome in headless mode (without graphical interface)        options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36')        service = Service(r'C:\Users\rocco\.wdm\drivers\chromedriver\win64\121.0.6167.184\chromedriver-win32/chromedriver.exe')  # Specify the path to Chrome driver        driver = webdriver.Chrome(service=service, options=options)        # Open the web page        driver.get(url)        # Wait until the span element is visible on the page        span_element = WebDriverWait(driver, 10).until(            EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div/div[1]/div[1]/div[2]/section/main/div/div[1]/div/div[2]/div[1]/div/div/div/span/span'))        )        # Extract text from the found element        views = span_element.text.strip()        # Close the browser        driver.quit()        return {'views': views}    except Exception as e:        return {'error': str(e)}if __name__ == "__main__":    url = 'https://www.instagram.com/reels/C3sokyzOXx1/?hl=es-la'    stats = get_reel_stats(url)    print(stats)

Despite my efforts, I'm unable to retrieve the statistics due to Instagram's warning. Has anyone found a solution to this issue or can suggest another way to obtain Instagram reels statistics using Python?

What I have tried:

I have attempted to use Selenium along with WebDriver for Chrome to retrieve Instagram reels statistics. I configured the script to open the Instagram reel page and search for the element containing the view statistics.

What I expected:

I expected the script to successfully extract the view statistics from the Instagram reel and print them to the console. However, instead of obtaining the statistics, I received a warning message from Instagram indicating that this action is a browser feature intended for developers.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>