-
-
Save cahyonobagus/7b7fd584f77affe43fae7b8d0c234cae to your computer and use it in GitHub Desktop.
sample web scrapping with Node.js, Axios and Cheerio
This file contains 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 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