pythonweb-scrapinginstagram

How to scrape instagram account info in python


I am trying to do something extremely simple in python yet somehow it's very difficult. All I want to do is write a python script that records the number of people a Instagram user is following, and the number of it's followers. That's it.

Can anyone point me to a good package to do this? preferably not beautiful soup as that is overly complicated for what I want to do. I just want something like

[user: example_user, followers:9019, following:217] 

Is there an Instagram specific python library?

The account I want to scrape is public. This is very simple to do for twitter.

Any help is appreciated.


Solution

  • As the content you look for are available in page source, you can fetch them using requests in combination with BeautifulSoup.

    Give it a try:

    import requests
    from bs4 import BeautifulSoup
    
    html = requests.get('https://www.instagram.com/michaeljackson/')
    soup = BeautifulSoup(html.text, 'lxml')
    item = soup.select_one("meta[property='og:description']")
    name = item.find_previous_sibling().get("content").split("•")[0]
    followers = item.get("content").split(",")[0]
    following = item.get("content").split(",")[1].strip()
    print(f'{name}\n{followers}\n{following}')
    

    Results:

    Name :Michael Jackson
    Followers :1.6m
    Following :4