Created
June 3, 2022 13:51
-
-
Save parnexcodes/2af8e3e00398fe057025caea111e18f0 to your computer and use it in GitHub Desktop.
imdb_top_250 scrape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios') | |
const cheerio = require('cheerio') | |
const express = require('express') | |
const app = express() | |
const port = 3000 | |
app.get('/', async (req, res) => { | |
const getMovies = async () => { | |
const siteUrl = 'https://www.imdb.com/chart/top/' | |
const { data } = await axios.get(siteUrl) | |
const $ = cheerio.load(data) | |
let finalData = [] | |
$('tbody.lister-list').find('tr').each((index, element) => { | |
let link = $(element).find('a').attr('href') | |
let image = $(element).find('img').attr('src') | |
let rank = index + 1 | |
let name = $(element).find('td.titleColumn').find('a').text() | |
let year = $(element).find('span.secondaryInfo').text().replace('(', '').replace(')', '') | |
let rating = $(element).find('td.ratingColumn.imdbRating').find('strong').text() | |
finalData.push({ | |
'rank': rank, | |
'name': name, | |
'year': year, | |
'rating': rating, | |
'link': `https://imdb.com${link}`, | |
'image': image, | |
}) | |
}) | |
return finalData | |
} | |
const apiData = await getMovies() | |
res.json({ | |
'data': apiData | |
}) | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening on port ${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment