SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

How to Build a Web Scraper Using Node.js

March 25, 2022 by Christopher Jan Benitez Leave a Comment

web scraping

Gathering information from online sources is a difficult task especially if you’re doing it manually. But with automation, you’ll be able to get all sorts of data that you might need whether it be for business research or personal use.

Today, we’ll tell you everything you need to learn about web scraping and Node.js so you too can automatically extract data from all over the web.

What Is a Web Scraper?

First, let’s talk about web scrapers. What is web scraping? And how can a web scraper help you gather data?

Web scraping is the process of gathering data from online sources — so long as the data is publicly available. This data can then be used by marketers, business owners, or researchers.

To get data, you’ll need a web scraper. A web scraper is a script that you run to crawl and collect publicly available information.

There are many types of scripts that you can use. But today, we’re using Node.js as our script’s foundation.

What Is Node.js?

Node.js is a free open-source server environment that uses JavaScript on the server. It can run on different platforms such as Windows, Linux, and Mac OS.

Some of its advantages include being very fast (as it uses Google Chrome’s V8 JavaScript engine), asynchronous, and event-driven. There’s also no buffering and it’s highly scalable.

How to Use Node.js to Create a Web Scraper

Before we begin, you’ll need to download and install Node.js. Those with some JavaScript knowledge will have an easier time following along.

You will also need access to a code editor like WebStorm or Visual Studio Code.

Step 1: Start a Project

Using your code editor of choice, start a new project by creating an empty directory. You can name the project anything you want.

how to build web scraper - create your project-min

This will allow you to create a completely new project from scratch.

Step 2: Create a JSON Package

Open the terminal and type the command npm init — the main purpose of this command is to allow us to install packages or modules into the project.

how to build web scraper - npm init-min

A package or module (in this context) refers to publicly available code that adds functionality to an application.

While this could be an oversimplification, you can think of packages like WordPress plugins that add more features to a WordPress site.

You can find a list of packages on the NPM Inc. website.

After typing npm init, press enter. This will enable you to create a package JSON file — a requirement for using Node.js.

You’ll be presented with fields that you can fill up to describe the package you’re creating (such as the package name, version, description, etc.). You can leave these blank by hitting enter a few times until you’re asked to confirm if this is okay.

how to build web scraper - package description-min

Confirm that everything is okay to continue.

Check your project to see if your JSON package was successfully created.

how to build web scraper - json package-min

Step 3: Create an Index.js file

The main file that we’re going to be reading is index.js. You can confirm this in the script you’ve just generated.

how to build web scraper - index-min

That means we’ll have to create an index.js file. To do that, right-click your package.json file and navigate to New > File.

how to build web scraper - new file-min

Name the new file index.js

Step 4: Install Packages

For this project, you will need to install some packages starting with one called Express. This package is a backend framework for Node.js

Type in the command npm i express then hit enter.

how to build web scraper - npm i express-min

Wait for Express to install. It should only take a couple of seconds. After installation, you should now see Express as a dependency for the project.

how to build web scraper - express dependency-min

Now that Express had been installed, we can move on to the next package that we need to install. This one is called Cheerio. This will be used to pick out HTML elements on a web page.

The command to install Cheerio is npm i cheerio. Follow the same steps you used to install Express.

The last package we’ll need is Axios — a promise-based HTTP client for the browser and Node.js

The command to install Axios is npm i axios. Follow the same steps as above to install.

Step 5: Write a Script

With all the packages installed, you’ll need to write a Start script.

Under Scripts, enter this command:

“start”: “nodemon index.js”

how to build web scraper - nodemon indexjs-min

This command looks at any changes made to the index.js file.

Step 6: Modify the Index.js File

To use all the packages you just installed, you’ll need to “require” them in the index.js file. Enter the following commands in the index.js file.

Const axios = require(‘axios’)

Const cheerio = require(‘cheerio’)

Const express = require(‘express’)

how to build web scraper - require-min

After that, you’ll need to “initialize” Express.

To do this, you’ll first need to “call” Express. Here’s the command:

Const PORT = 8000

Const axios = require(‘axios’)

Const cheerio = require(‘cheerio’)

Const express = require(‘express’)

Const app = express(

App.listen(PORT, () => console.log(‘server running on PORT ${PORT}’) )

how to build web scraper - port-min

Note: The values for app and port can be anything you want.

To check your progress, you’ll need to run the app. Since we used the command Start earlier, the command you’ll need to use is npm run start

how to build web scraper - check app-min

If you’ve been following along and used the same values as in the example, you should receive a result that says “Server running on PORT 8000”

how to build web scraper - running on port 8000-min

Step 7: Start Scraping

Now we can begin scraping data. To do that, we’ll need to make use of the packages we installed.

In the index.js file, insert these commands:

Const url = ‘https://www.example.com’

Axios(url)

.then(response => {

Const html = response.data

Console.log(html)

})

how to build web scraper - scraping data-min

Note: The value “https://www.example.com” should be replaced with the site you’re trying to extract data from.

This will extract data from the entire webpage that you entered. If you want to extract specific data, you’ll have to use the Cheerio package.

Let’s use an example.

If you want to grab the title of each article on The Guardian’s website (along with their URLs), you can use this syntax:

Const url = ‘https://www.theguardian.com/uk’

axios(url)

.then(response => {

Const html = response.data

Const $ = cheerio.load(html)

Const articles = []

$(‘.fc-item__title’, html).each(function() {

Const title = $(this).text()

Const url = $(this).find(‘a’).attr(‘href)

Articles.push({

Title,

URL

{)

console.log(articles)

}).catch(err => console.log(err))

how to build web scraper - cheerio syntax-min

You’ll have to use the Inspect Element feature of your browser to see which values you need to enter to specify which data you can extract from the page.

Conclusion

You can now modify the code to do whatever you need. With some tweaking and practice, you’ll be able to extract a ton of data from online sources.

Screenshots used are from Code With Ania Kubow

Related posts:

Designing With Data: How to Improve UX Using Customer Data

How To Collect and Transform Data Into Your Business Value

Filed Under: Development, Tutorial Tagged With: Node.js, web scraper

Leave a Reply

Your email address will not be published. Required fields are marked *

Please prove you're human *

Recent Posts

  • How to use a PDF file combiner to support a Web planner optimally
  • What Factors Determine the Best Digital Marketing Agency?
  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js

Archives

  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022