Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Python 3 Flask Instagram API Project to Scrape Followers & Following and Posts and Display it inside Table

Posted on May 15, 2023

 

 

Welcome folks today in this blog post we will be using the instagram api in flask to download all images and video posts from insta profile and export it as zip file in flask using python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new flask project using the below command as shown below

 

 

pip install flask

 

 

pip install instaloader

 

 

pip install python-dotenv

 

 

And after that you will see the below directory structure of the flask app as shown below

 

 

 

Now we need to create the .env file and store the login credentials as shown below

 

 

1
2
EMAIL=##youremail##
PASSWORD=##yourpassword##

 

 

And now inside the app.py file you need to copy paste the below code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, render_template, request
import instaloader
from dotenv import load_dotenv
import os
 
 
app = Flask(__name__)
 
@app.route("/")
def home():
    return render_template("home.html")
 
@app.route("/profile", methods=["POST"])
def profile():
    # Get the username and password from the form
    username = request.form["username"]
 
    email = os.getenv("EMAIL")
    password = os.getenv("PASSWORD")
    
    # Create an Instaloader instance
    L = instaloader.Instaloader()
 
    # Login to the account
    L.login(email,password)
 
    # Load the profile of the user
    profile = instaloader.Profile.from_username(L.context, username)
 
    # Get the number of posts, followers, and following
    num_posts = profile.mediacount
    num_followers = profile.followers
    num_following = profile.followees
 
    # Get the names of the people the user is following
    following_names = []
    for following in profile.get_followees():
        following_names.append(following.username)
 
    # Get the names of the followers
    followers_names = []
    for follower in profile.get_followers():
        followers_names.append(follower.username)
 
    return render_template("profile.html", username=username, num_posts=num_posts,
                           num_followers=num_followers, num_following=num_following,
                           following_names=following_names, followers_names=followers_names)
 
if __name__ == "__main__":
    app.run(debug=True)

 

 

As you can see we have started the flask server at the port number 5000 and then we are displaying the home.html template when the user goes to the / route and then we have the post request to scrape the names of the followers and the following and also the number of posts.

 

And now we need to make the templates folder and inside it we need to make the home.html file and copy paste the following code

 

 

templates/home.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
  <head>
    <title>Instaloader Profile</title>
  </head>
  <body>
    <h1>Instaloader Profile</h1>
    <form action="{{ url_for('profile') }}" method="POST">
      <label for="username">Username:</label>
      <input type="text" id="username" name="username">
      <button type="submit">Submit</button>
    </form>
  </body>
</html>

 

 

 

 

 

Now we need to make the profile.html template inside the templates folder and copy paste the below code

 

 

templates/profile.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="container">
<h1 class="text-center">Profile: {{ username }}</h1>
 
<table class="table table-striped">
  <tr>
    <th>Number of Posts</th>
    <th>Number of Followers</th>
    <th>Number of Following</th>
  </tr>
  <tr>
    <td>{{ num_posts }}</td>
    <td>{{ num_followers }}</td>
    <td>{{ num_following }}</td>
  </tr>
</table>
<h2>Download Excel File:</h2>
<h2>Following</h2>
<ul>
  {% for following_name in following_names %}
  <li>{{ following_name }}</li>
  {% endfor %}
</ul>
 
<h2>Followers</h2>
<ul>
  {% for follower_name in followers_names %}
  <li>{{ follower_name }}</li>
  {% endfor %}
</ul>
</div>

 

 

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme