web scriping in javascript

 npm init -y     type this in terminal

npm install request


npm js request


it should contain a function with a request URL and one callback function 

with the help of cheerio we can manipulate the data 

const request = require('request');
request('http://www.google.com', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});







/////////////////////////

// const request = require('request');
// const requre = require("cheerio");

// const fs = require("fs");
// request("https://www.espncricinfo.com/series/ipl-2021-1249214/punjab-kings-vs-delhi-capitals-29th-match-1254086/ball-by-ball-commentary",callback);


// //
// function callback(error, response, html) {
//     if(!error){
//     const manipulationTool = cheerio.load(html); 

//     let comments = manipulationTool("match-comment-long-text p");
//     // console.log(comments);

//     let reqComment = manipulationTool(comments[0].text());
//     console.log(reqComment);
//     // fs.writeFileSync("google.html",html);
// //   console.log('body:', body); // Print the HTML for the Google homepage.
// }
// }

// //null is a false state and !null is true

const request = require("request");
const cheerio = require("cheerio");

request(
  "https://www.espncricinfo.com/series/ipl-2021-1249214/punjab-kings-vs-delhi-capitals-29th-match-1254086/ball-by-ball-commentary",
  callback
);

function callback(errorresponsehtml) {
  if (!error) {
    const manipulationTool = cheerio.load(html);

    let comments = manipulationTool(
      ".col-14.col-md-15.col-lg-14 .match-comment-long-text p"
    );

    let reqComment = manipulationTool(comments[0]).text();
    console.log(reqComment);
  }
}









const request = require("request");
const cheerio = require("cheerio");
const { text } = require("cheerio/lib/static");

request(
  "https://www.espncricinfo.com/series/ipl-2021-1249214/punjab-kings-vs-delhi-capitals-29th-match-1254086/full-scorecard",
  callback
);

function callback(errorresponsehtml) {
  if (!error) {
    const manipulationTool = cheerio.load(html);

    let bothTable = manipulationTool(".table.bowler");

    let player = "";
    let maxwicket = 0;

    for(let i = 0;i<bothTable.length;i++){
        let tablerow = manipulationTool(bothTable[i]).find("tbody tr");
        for(let j = 0;j<tablerow.length;j++){
            let allRowColumns = manipulationTool(tablerow[j]).find("td");
            let currplayername = manipulationTool(allRowColumns[0]).text();
            let currplayerwicket = manipulationTool(allRowColumns[4]).text();
            if(maxwicket<currplayerwicket){
                maxwicket = currplayerwicket;
                player = currplayername;
            }
        }
    }
console.log(player);
  }
}





Comments

Popular posts from this blog

Git and Github

bubble short Q1

Java Program to Check Whether the String Consists of Special Characters