Back to articles
Web Scraping for Beginners: Your First Scraper in 10 Minutes (Node.js)

Web Scraping for Beginners: Your First Scraper in 10 Minutes (Node.js)

via Dev.to TutorialАлексей Спинов

Never scraped a website before? This guide gets you from zero to extracting real data in 10 minutes. Step 1: Set Up (1 minute) mkdir my-first-scraper && cd my-first-scraper npm init -y npm install cheerio Step 2: Write the Scraper (3 minutes) Create scraper.js : const cheerio = require ( ' cheerio ' ); async function scrape () { // Fetch a webpage const url = ' https://news.ycombinator.com ' ; const res = await fetch ( url ); const html = await res . text (); // Parse the HTML const $ = cheerio . load ( html ); // Extract data const stories = []; $ ( ' .titleline > a ' ). each (( i , el ) => { stories . push ({ rank : i + 1 , title : $ ( el ). text (), url : $ ( el ). attr ( ' href ' ) }); }); // Display results console . log ( `Found ${ stories . length } stories:\n` ); stories . slice ( 0 , 10 ). forEach ( s => { console . log ( ` ${ s . rank } . ${ s . title } ` ); }); return stories ; } scrape (); Step 3: Run It (1 minute) node scraper.js Output: Found 30 stories: 1. Show HN: I bui

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
6 views

Related Articles