Node.js Express Web Scraping Project to Display Comments of Video in Table Using EJS & JS
const ytcm = require("@freetube/yt-comment-scraper");
const express = require("express");
const bodyparser = require('body-parser')
const getvideoid = require('get-video-id')
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",{comments:''});
});
app.post("/getcomments", (req, res) => {
let url = req.body.url
let {id} = getvideoid(url)
const payload = {
videoId: id, // Required
sortByNewest: true,
};
ytcm
.getComments(payload)
.then((data) => {
console.log(data.comments);
res.render('index',{comments:data.comments})
})
.catch((error) => {
console.log(error);
});
});
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>Youtube Video Comment 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">Youtube Video Comment Scraper</h1>
<form action="/getcomments" method="post">
<div class="form-group">
<label for="url">Enter Youtube Video URL:</label>
<input
class="form-control"
type="text"
name="url"
required
placeholder="https://www.youtube.com/watch?v=ugiFpA03B6c"
id=""
/>
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">Get Comments</button>
</div>
</form>
<%if(comments){%>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Comment</th>
<th>Published Time</th>
<th>Likes</th>
</tr>
</thead>
<tbody>
<%comments.forEach(comment => {%>
<tr>
<td><%=comment.author%></td>
<td><%=comment.text%></td>
<td><%=comment.time%></td>
<td><%=comment.likes%></td>
</tr>
<%})%>
</tbody>
</table>
<%}%>
</div>
</body>
</html>