Python Script to Fetch Instagram Tageted User's Followers and Following and Store in Excel - Timeouts Encountered - Stac

admin2025-04-25  3

I am share the part of code in my program to fetch the follower and following lists of a specific Instagram user and store the data in an Excel sheet. I'm using Selenium to interact with the Instagram website.

However, I'm encountering a "Timed out while waiting for page to load" error when trying to access the target user's profile.

Code

from selenium import webdriver
from seleniummon.exceptions import NoSuchElementException
from seleniummon.exceptions import NoSuchElementException, TimeoutException
from selenium.webdrivermon.keys import Keys
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas as pd

def get_user_info(username, driver):
    try:
        driver.get(f"/{username}/")
        WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "span._ac2a"))        
        
        )
        followers_str = driver.find_element(By.CSS_SELECTOR, "span._ac2a").text
        following_str = driver.find_element(By.CSS_SELECTOR, "span._ac2b").text
        followers = int(followers_str.replace(',', ''))
        following = int(following_str.replace(',', ''))
        return followers, following
    except NoSuchElementException:
        print(f"Follower/Following count elements not found for {username}.")
        return None, None
    except TimeoutException:
        print(f"Timed out while waiting for page to load for {username}.")
        return None, None
    except ConnectionError:
        print(f"Connection error while fetching data for {username}.")
        return None, None
    except Exception as e:
        print(f"Error fetching info for {username}: {e}")
        return None, None

def login_to_instagram(username, password, driver):
   
    try:
        driver.get("/")
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.NAME, "username"))
        )
        username_field = driver.find_element(By.NAME, "username")
        username_field.send_keys(username)
        password_field = driver.find_element(By.NAME, "password")
        password_field.send_keys(password)
        password_field.send_keys(Keys.RETURN)
        time.sleep(5)  
    except Exception as e:
        print(f"Login failed: {e}")

def main():
    
    instagram_username = "Abcd" 
    instagram_password = "XXXX" 
    target_username = "Trg_user_id"

    driver = webdriver.Chrome()  

    try:
        login_to_instagram(instagram_username, instagram_password, driver)
        followers, following = get_user_info(target_username, driver)

        data = {'Username': [target_username], 'Followers': [followers], 'Following': [following]}
        df = pd.DataFrame(data)

        df.to_excel('instagram_user_data.xlsx', index=False)
        print("Data saved to instagram_user_data.xlsx")

    finally:
        driver.quit()

if __name__ == "__main__":
    main()

Error Message:

DevTools listening on ws://127.0.0.1:55191/devtools/browser/971e8f47-67b5-46d5-93a8-e4c625548d1e Created TensorFlow Lite XNNPACK delegate for CPU. Timed out while waiting for page to load for Trg_user_id. Data saved to instagram_user_data.xlsx

I've tried:

Increasing the timeout in WebDriverWait. Checking for network issues and Instagram server stability. I suspect the issue might be related to Instagram's rate limiting or changes in their website structure. Any insights or suggestions on how to resolve this timeout issue would be greatly appreciated.

转载请注明原文地址:http://anycun.com/QandA/1745529817a90819.html