rittam
rittam

Reputation: 318

How to get the number of post a user has in instagram?

I am creating a instagram bot using selenium python. I just wanted to know how to retrieve the number of the post a user contain in their profile page. So that I can handle the "NO POSTS YET" condition in my script.

Can anyone give me a hint. I don't need a complete code or just hint me to the right place.

Example profile : https://www.instagram.com/michelemessina/

Upvotes: 0

Views: 1501

Answers (3)

Oleksandr Makarenko
Oleksandr Makarenko

Reputation: 808

According to your task I wrote a few lines that get amount of posts, followers and following. You can run the code to view results.

browser = webdriver.Chrome()
browser.get('https://www.instagram.com/michelemessina/')
wait = WebDriverWait(browser, 5)

posts = wait.until(EC.presence_of_element_located((By.XPATH, '//li/span[text()=" posts"]/span'))).text
followers = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" followers"]/span'))).text
following = wait.until(EC.presence_of_element_located((By.XPATH, '//li/a[text()=" following"]/span'))).text
print('Posts: {}; Followers: {}; Following: {}.'.format(posts, followers, following))

Hope, this will help you.

Upvotes: 1

hyukkyulee
hyukkyulee

Reputation: 1194

I prefer to use find_element_by_xpath since xpath can be easily found in chrome.

Here's how: right click -> inspect -> right click -> Copy -> CopyXpath

xpath = '//*[@id="react-root"]/section/main/div/header/section/ul/li[1]/span/span' if browser.find_element_by_xpath(xpath).text == '0': pass

Upvotes: 1

Andrei
Andrei

Reputation: 5647

You can get the text of the element via xPath:

//span[text() = ' posts']/span

img

Upvotes: 1

Related Questions