Node.JS Puppeteer IMDb Web Scraping Project to Display Movie Details in Table Using EJS & Express

Node.js Puppeteer IMDb Web Scraping Project to Display Movie Details in Table Using EJS & Express

const puppeteer = require("puppeteer");
const express = require('express')
const bodyparser = require('body-parser')
    
const app = express()
 
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
        
app.set('view engine', 'ejs')
        
app.get('/', (req, res) => {
        res.render('index',{data:''})
    })
 
app.post('/getinfo', async (req, res) => {
    const browser = await puppeteer.launch({
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto(req.body.url);
 
  await page.waitForSelector(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > h1"
  );
 
  let title = await page.$(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > h1"
  );
  let titleinfo = await page.evaluate((el) => el.textContent, title);
  console.log(titleinfo);
 
  await page.waitForSelector(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > div > ul > li:nth-child(1) > a"
  );
 
  let releasedate = await page.$(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > div > ul > li:nth-child(1) > a"
  );
  let date = await page.evaluate((el) => el.textContent, releasedate);
  console.log(date);
 
  await page.waitForSelector(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > div > ul > li:nth-child(3)"
  );
 
  let timeofmovie = await page.$(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-94726ce4-1.iNShGo > div > ul > li:nth-child(3)"
  );
 
  let timeofmovieinfo = await page.evaluate(
    (el) => el.textContent,
    timeofmovie
  );
 
  console.log(timeofmovieinfo);
 
  await page.waitForSelector(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-db8c1937-0.eGmDjE.sc-94726ce4-4.dyFVGl > div > div:nth-child(1) > a > div > div > div.sc-7ab21ed2-0.fAePGh > div.sc-7ab21ed2-2.kYEdvH > span.sc-7ab21ed2-1.jGRxWM"
  );
 
  let ratingofmovie = await page.$(
    "#__next > main > div > section.ipc-page-background.ipc-page-background--base.sc-c7f03a63-0.kUbSjY > section > div:nth-child(4) > section > section > div.sc-94726ce4-0.cMYixt > div.sc-db8c1937-0.eGmDjE.sc-94726ce4-4.dyFVGl > div > div:nth-child(1) > a > div > div > div.sc-7ab21ed2-0.fAePGh > div.sc-7ab21ed2-2.kYEdvH > span.sc-7ab21ed2-1.jGRxWM"
  );
 
  let ratingofmovieinfo = await page.evaluate(
    (el) => el.textContent,
    ratingofmovie
  );
 
  console.log(ratingofmovieinfo);
 
    await browser.close();
 
    const data = {
        title: titleinfo,
        release: date,
        time: timeofmovieinfo,
        rating:ratingofmovieinfo
    }
    
    res.render('index',{data:data})
})
 
app.listen(5000, () => {
    console.log("App is listening on port 5000")
})

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>IMDb Movie Scraper</title>
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
    />
  </head>
  <body>
    <div class="container">
      <h1 class="text-center">IMDb Movie Scraper</h1>
      <form action="/getinfo" method="post">
          <div class="form-group">
              <label for="url">Enter Movie URL:</label>
              <input type="text" name="url" class="form-control" placeholder="Enter Movie URL" required id="">
          </div>
          <div class="form-group">
              <button class="btn btn-danger btn-block">
                  Get Info
              </button>
          </div>
      </form>
 
      <%if(data){%>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Title of Movie</th>
                        <th>Release Date</th>
                        <th>Time of Movie</th>
                        <th>Rating of Movie</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><%=data.title%></td>
                        <td><%=data.release%></td>
                        <td><%=data.time%></td>
                        <td><%=data.rating%></td>
                    </tr>
                </tbody>
            </table>
        <%}%>
    </div>
  </body>
</html>

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment