Nodejs 2020 - Best for auto programming



By sending HTTP request to a particular URL


You need to understand the requirements to create a valid http request
You are proficient in "curl", this will be a useful tool: https://tool.baoxinh.com/app/curl-to-code.cg
  • request: For sending HTTP request to the URL 
var request = require('request');

var headers = {
    'authority': 'www.nodejsauto.com',
    'cache-control': 'max-age=0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
    'sec-fetch-dest': 'document',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    //'cookie': '***',
};

var options = {
    url: 'https://www.nodejsauto.com/',
    headers: headers
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        require('fs').writeFileSync(__dirname+ '/data.txt', body);
    }
}

request(options, callback);

And then by extracting HTML of that web page for getting useful information is known as crawling or web scraping.

You should be familiar this with "jquery".




  • cheerio: For parsing DOM and extracting HTML of web page

  • var cheerio= require('cheerio');
    var fileData= __dirname+ '/data.txt';
    var content= require('fs').readFileSync(fileData).toString();
    var $= cheerio.load(content);
    var title= $('title').text().trim();
    console.log({title});
    
    


    Please wait for next!

    Comments