Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Ghodawalaaman/57cc27a1071517408edeb18253a5f4b1 to your computer and use it in GitHub Desktop.

Select an option

Save Ghodawalaaman/57cc27a1071517408edeb18253a5f4b1 to your computer and use it in GitHub Desktop.
Instagram scraping with Selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.edge.options import Options
from selenium.webdriver.chrome.options import Options
import time
# Path to the Chrome user profile directory
user_data_dir = "/tmp/" # Replace with your profile path
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
chrome_options.add_argument("--headless") # Uncomment for headless mode
driver = webdriver.Edge(options=chrome_options)
driver.get("https://www.instagram.com/")
time.sleep(2)
try:
username = driver.find_element(By.XPATH, "//input[@name='username']")
password = driver.find_element(By.XPATH, "//input[@name='password']")
submit_button = driver.find_element(By.XPATH, "//button[@type='submit']")
username.send_keys("testa_ccount7482") # Replace with your username
password.send_keys("BkYGMDNTa.+Lg4E") # Replace with your password
submit_button.click()
except Exception as e:
print("already logged in or error finding elements")
time.sleep(5)
body = driver.find_element(By.TAG_NAME, "body")
urls = set()
for _ in range(10):
body.send_keys(Keys.PAGE_DOWN)
imgs = driver.find_elements(By.XPATH, "//img")
try:
for img in imgs:
try:
src = img.get_attribute("src")
urls.add(src)
# TODO: remove the following line
except Exception as e:
print(f"Error retrieving URL")
except Exception as e:
print(f"Error finding images")
time.sleep(1) # Adjust the sleep time as needed to load more content
print(f"Total unique URLs found: {len(urls)}")
with open("urls.txt", "a") as f:
for url in list(urls):
f.write(str(url) + "\n")
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment