Fireship · @fireship
A Guide to Web Scraping with Node.js
Uploaded July 24, 2019 · Length 9:54
Summary
Learn how to extract data from websites using Node.js web scraping techniques, including building a link preview in a front-end application and scraping Instagram.
- nodejs web scraping
- link preview
- cloud function
- javascript scraping
- instagram scraping
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
Today's video is all about extracting data from the web.
- 0:03
In a perfect world, every website provides an easy-to-use API that provides access to
- 0:07
the data that you want.
- 0:08
But unfortunately, we don't live in a perfect world.
- 0:11
But as hackers, we can resort to a technique known as web scraping to take the data that
- 0:14
we need by brute force.
- 0:16
And I know that sounds kind of shady, but there are plenty of valid use cases for web
- 0:19
scraping, as we'll see in this video.
- 0:21
For example, we'll build a Node.js Cloud Function that builds a link preview in a front-end
- 0:24
application.
- 0:25
And then we'll move on to a more questionable use case where we actually log into an Instagram
- 0:29
count and interact with the app as if we were a real logged in user.
- 0:32
If you're new here, like and subscribe, and congrats to Alex Karpov, you're the t-shirt
- 0:36
winner from last week's video.
- 0:37
But there's another one going out with this video, all you have to do is leave a comment
- 0:40
below.
- 0:41
Now in this video, we're going to look at two very different strategies for web scraping
- 0:44
using JavaScript and Node.js.
- 0:46
The first use case that we'll look at is building link previews in a front-end application.
- 0:50
You see this in applications like Twitter and Slack where the user includes a URL in
- 0:54
the post and then it will automatically generate a preview for that URL.
- 0:58
We can implement this feature from scratch using a Node.js Cloud Function.
- 1:02
And the reason we need to do this with Node.js and not do it entirely from the front end is
- 1:05
because the browser will block cross-origin requests.
- 1:09
So if your website has a script that tries to make a request to a different website, you'll
- 1:13
end up with this CORS error in the browser.
- 1:15
One of the main reasons that browsers block cross-origin requests is because of the Spectre
- 1:18
security vulnerability.
- 1:20
It's beyond the scope of this video, but basically making a client-side request to
- 1:23
a third-party website could allow an attacker to read what's in memory on the client's
- 1:28
device.
- 1:29
But we can easily overcome this limitation by creating a Cloud Function that will serve
- 1:31
as a middleman.
- 1:32
On our frontend application, the user might paste a couple of URLs into a form, then we'll
- 1:37
send those URLs to our Cloud Function, which will then make the request to those third-party
- 1:41
websites in an isolated environment.
- 1:43
And this is where the web scraping comes in.
- 1:45
Because the Cloud Function is going to retrieve an HTML document, which is just a big string
- 1:49
of HTML, then it will process that HTML looking for meta tags that we can use for the link
- 1:54
preview, things like the description, title, and featured image.
- 1:57
So let's go ahead and jump into the code, and the first thing we'll do is initialize


