corsparser

corsparser

by Gopinath Kaliappan

JavaScript

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app     = express();



app.get('/axios-scrape', function(req,res) {

    const axios = require('axios');
    const cheerio = require('cheerio');

    // const url = 'https://www.indiatoday.in/india/story/delhi-cop-suspended-for-training-his-children-at-shooting-range-1601113-2019-09-20';
    const url = req.query.url;
    console.log(url)

    axios(url)
      .then(response => {
        const html = response.data;
        const $ = cheerio.load(html)
        const statsTable = $('.statsTableContainer > tr');
        const topPremierLeagueScorers = [];
        const headers = [];
        const titleHeader = $('h1' );
        const content = $('.description').text();
        const images = $('img');
        const imgs = [];
        // res.send(titleHeader);
        // const title = $('.post-full-title');
        titleHeader.each(function() {
                const tit = $(this).text();
                headers.push({tit});
        })     
        images.each(function() {
                            const img = $(this).text();
                imgs.push({img});
        })

        statsTable.each(function () {
          const rank = $(this).find('.rank > strong').text();
          const playerName = $(this).find('.playerName > strong').text();
          const nationality = $(this).find('.playerCountry').text();
          const goals = $(this).find('.mainStat').text();

          topPremierLeagueScorers.push({
            rank,
            name: playerName,
            nationality,
            goals,
          });
        });

        res.send({ headers, content,  imgs });
      })
      .catch(console.error);
});


app.get('/scrape', function(req, res){
console.log({})
url = 'https://www.freecodecamp.org/news/the-ultimate-guide-to-web-scraping-with-node-js-daa2027dcd3/';

request(url, function(error, response, html){
    if(!error){
      ...