Skip to content

Instantly share code, notes, and snippets.

@arseniyturin
Last active February 5, 2023 16:16
Show Gist options
  • Save arseniyturin/3491f98dcecb8ed942a9d14f73c285ea to your computer and use it in GitHub Desktop.
Save arseniyturin/3491f98dcecb8ed942a9d14f73c285ea to your computer and use it in GitHub Desktop.
How to retrieve instagram post data with Selenium
from selenium import webdriver
driver = webdriver.Chrome('../chromedriver')
driver.get('https://www.instagram.com/accounts/login/')
def get_post_description():
description = driver.execute_script('''return document.getElementsByClassName('C4VMK')[0].getElementsByTagName('span')[0].innerText''')
return description
def get_post_date():
date = driver.execute_script('''return document.getElementsByClassName('_1o9PC Nzb55')[0].getAttribute('datetime')''')
return date
def get_total_likes():
try:
likes = driver.execute_script('''return document.getElementsByClassName('Nm9Fw')[0].lastElementChild.getElementsByTagName('span')[0].innerText''')
likes = [i.split('\n')[0] for i in likes]
likes = ', '.join(likes)
return likes
except:
return 0
@Mehmet-Erkan
Copy link

This will not work persistent since the class names change from time to time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment