Node.js Express Scrape Instagram User Profile Information Using Axios Library Full Project For Beginners

Node.js Express Scrape Instagram User Profile Information Using Axios Library Full Project For Beginners

 

Welcome folks today in this tutorial we will be scraping instagram user profile in node.js and express using axios library. All the source code of the application is given below.

 

 

Get Started

 

 

In order to get started we need to initialize a node.js project with the following command

 

npm init -y

 

Now we need to install the following dependencies

 

npm i express

 

npm i ejs

 

npm i nodemon

 

npm i axios

 

 

Now make a views folder and inside it make a index.ejs file and copy paste the following code

 

 

index.ejs

 

 

<!DOCTYPE html>
<html>
  <head>
    <title><%=title%></title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
      <div class="container">
          <h1 class="text-center">Instagram User Information App</h1>
          <br><br>
          <form id="form">
              <div class="form-group">
                  <label for="instagramusername">Instagram Username:</label>
                  <input type="text" name="" class="form-control" id="username" required placeholder="Enter Instagram Username">
              </div>
              <div class="form-group">
                  <button id="button" class="btn btn-danger btn-block">
                      Get User Instagram Data
                  </button>
              </div>
          </form>
          <div id="result"></div>
      </div>
</body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script>
      var result,name
      $("#form").submit(function(e){
          e.preventDefault()

          name = $("#username").val()

          $("#button").text("Please wait fetching channel info")

          $("#button").prop("disabled","true")

          $("#result").empty()

          name = name.trim()

          console.log(name)

          $.ajax({
              method:"POST",
              url:"/getinstagraminfo",
              data:{username:name},
              success:function(data){
                  console.log(data.stats)


                  $("#button").text("Get Channel Info")

                  $("#button").removeAttr("disabled")

                  $("#username").val("")

                  result = `
                
                <table class="table table-striped">
    <thead>
      <tr>
        <th>Full Name</th>
        <th>User Profile Picture</th>
        <th>Profile Link</th>
        <th>No of Followers</th>
        <th>Following Users</th>
        <th>Private Account Status</th>
        <th>Verified Account Status</th>
        <th>Username</th>
        <th>No of Posts</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>${data.fullname}</td>
        <td><img class="img-rounded" src="${data.profile_pic}"/></td>
        <td><a target="_blank" class="btn btn-danger" href="https://www.instagram.com/${name}">Go to Profile</a></td>
        <td>${data.followers}</td>
        <td>${data.following}</td>
        <td>${data.private}</td>
        <td>${data.verified}</td>
        <td>${data.user_name}</td>
        <td>${data.posts}</td>
      </tr>
    </tbody>
  </table>     
                `
                $("#result").append(result)
            


              }
          })
      })
  </script>
</html>

 

READ  How to Convert CSV File to JSON File in Node.js Using CSVTOJSON Library Full Example

 

 

Now we will make a index.js file and copy paste the following code

 

 

const axios = require(`axios`)

const express = require('express')

const bodyparser = require('body-parser')

const app = express()

app.use(bodyparser.json())

app.use(bodyparser.urlencoded({extended:false}))

app.set("view engine","ejs")

app.get('/instagramuserprofile',(req,res) => {

    res.render('index',{title:'FREE Instagram User Profile Information Finder App Online - FreeMediaTools.com'})

})

app.post('/getinstagraminfo',async(req,res) => {
    var username = req.body.username 

    console.log(us)
    
    try {
        const {
        data
        } = await axios.get(`https://www.instagram.com/${username}/?__a=1`)
        user = data.graphql.user
        console.log(user)
        let followers = user.edge_followed_by.count
        let following = user.edge_follow.count
        let fullname = user.full_name
        let user_name = user.username
        let verified = user.is_verified
        let private = user.is_private
        let posts = user.edge_owner_to_timeline_media.count
        let profile_pic = user.profile_pic_url_hd
        console.log(`${user_name} has ${followers} and follows ${following}. His full name is ${fullname}. His pic is ${profile_pic}`)
        res.json({
            followers:followers,
            following:following,
            fullname:fullname,
            user_name:user_name,
            profile_pic:profile_pic,
            verified:verified,
            private:private,
            posts:posts
        })
        } catch (error) {
        console.log(`USER NOT FOUND`)
        // throw Error(error);
        }

})

app.listen(5000,() =>{
    console.log(`App is listening on port 5000`)
})

 

READ  Build a Youtube Video Thumbnail Generator in HTML5 CSS3 and Javascript Full Project For Beginners

 

 

Screenshot

 

 

 

 

 

Leave a Reply