-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-crawler.js
39 lines (34 loc) · 1.15 KB
/
metadata-crawler.js
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
// Import the axios and cheerio libraries
const axios = require("axios");
const cheerio = require("cheerio");
// Define a function that takes a URL as input and returns a promise with the metadata
function getMetadata(url) {
// Make a GET request and parse the HTML response
return axios.get(url).then((response) => {
const html = response.data;
const $ = cheerio.load(html);
// Define the selectors for the metadata elements
const titleSelector = "head > title";
const descriptionSelector = "meta[name=description]";
const keywordsSelector = "meta[name=keywords]";
// Extract the metadata from the HTML
const title = $(titleSelector).text();
const description = $(descriptionSelector).attr("content");
const keywords = $(keywordsSelector).attr("content");
// Return an object with the metadata
return {
url: url,
title: title,
description: description,
keywords: keywords,
};
});
}
// Call the function with an example URL and print the result
getMetadata("https://www.bing.com")
.then((metadata) => {
console.log(metadata);
})
.catch((error) => {
console.error(error);
});