Skip to content

Instantly share code, notes, and snippets.

@cahyonobagus
Last active May 27, 2023 12:10
Show Gist options
  • Save cahyonobagus/7b7fd584f77affe43fae7b8d0c234cae to your computer and use it in GitHub Desktop.
Save cahyonobagus/7b7fd584f77affe43fae7b8d0c234cae to your computer and use it in GitHub Desktop.
sample web scrapping with Node.js, Axios and Cheerio
const axios = require('axios')
const cheerio = require('cheerio')
const scrape = () => {
axios.get('https://news.ycombinator.com/')
.then(({ data: page }) => {
const result = extractData(page)
console.log(result)
})
.catch(error => {
console.error(error)
})
}
const extractData = (page) => {
const $ = cheerio.load(page)
const $newsList = $('.titleline > a')
const result = []
for (const $news of $newsList) {
const title = $news.children[0].data
const url = $news.attribs.href
result.push({ title, url })
}
return result
}
scrape()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment