scrape headline from new york times in javascript

Here's an example code snippet that uses the request and cheerio modules in Node.js to scrape the headline from the New York Times website:

index.tsx
const request = require('request');
const cheerio = require('cheerio');

const url = 'https://www.nytimes.com/';

request(url, (error, response, body) => {
  if (!error && response.statusCode == 200) {
    const $ = cheerio.load(body);
    const headline = $('h2.css-1bxzzgs.e1voiwgp0').text(); // CSS selector for headline
    console.log("Headline: " + headline);
  } else {
    console.error(error);
  }
});
411 chars
15 lines

This code sends a GET request to the New York Times website and loads the HTML response with Cheerio. Then, it uses a CSS selector to extract the text of the headline element and logs it to the console.

gistlibby LogSnag