Last active
February 5, 2023 16:16
-
-
Save arseniyturin/3491f98dcecb8ed942a9d14f73c285ea to your computer and use it in GitHub Desktop.
How to retrieve instagram post data with Selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will not work persistent since the class names change from time to time