Study/Node.js

Node.js로 웹 스크랩핑 해보기

AC 2021. 11. 11. 23:34

준비물 : NodeJS

API 설치 axios, cheerio

npm i axios cheerio

const axios = require('axios')
const cheerio = require('cheerio')
const url ='https://www.theguardian.com/uk'
axios(url)
    .then(response =>{
        const html = response.data
        const ele =cheerio.load(html)
        const article =[]
        ele('.fc-item__title', html).each(function(){
           const title = ele(this).text()
           const url = ele(this).find('a').attr('href')
            article.push({
                title,
                url
            })
        })
       console.log(article)
    }).catch(err=> console.log(err) )
LIST