Fireship · @fireship

A Guide to Web Scraping with Node.js

Uploaded July 24, 2019 · Length 9:54

A Guide to Web Scraping with Node.js

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.

  1. 0:00

    Today's video is all about extracting data from the web.

  2. 0:03

    In a perfect world, every website provides an easy-to-use API that provides access to

  3. 0:07

    the data that you want.

  4. 0:08

    But unfortunately, we don't live in a perfect world.

  5. 0:11

    But as hackers, we can resort to a technique known as web scraping to take the data that

  6. 0:14

    we need by brute force.

  7. 0:16

    And I know that sounds kind of shady, but there are plenty of valid use cases for web

  8. 0:19

    scraping, as we'll see in this video.

  9. 0:21

    For example, we'll build a Node.js Cloud Function that builds a link preview in a front-end

  10. 0:24

    application.

  11. 0:25

    And then we'll move on to a more questionable use case where we actually log into an Instagram

  12. 0:29

    count and interact with the app as if we were a real logged in user.

  13. 0:32

    If you're new here, like and subscribe, and congrats to Alex Karpov, you're the t-shirt

  14. 0:36

    winner from last week's video.

  15. 0:37

    But there's another one going out with this video, all you have to do is leave a comment

  16. 0:40

    below.

  17. 0:41

    Now in this video, we're going to look at two very different strategies for web scraping

  18. 0:44

    using JavaScript and Node.js.

  19. 0:46

    The first use case that we'll look at is building link previews in a front-end application.

  20. 0:50

    You see this in applications like Twitter and Slack where the user includes a URL in

  21. 0:54

    the post and then it will automatically generate a preview for that URL.

  22. 0:58

    We can implement this feature from scratch using a Node.js Cloud Function.

  23. 1:02

    And the reason we need to do this with Node.js and not do it entirely from the front end is

  24. 1:05

    because the browser will block cross-origin requests.

  25. 1:09

    So if your website has a script that tries to make a request to a different website, you'll

  26. 1:13

    end up with this CORS error in the browser.

  27. 1:15

    One of the main reasons that browsers block cross-origin requests is because of the Spectre

  28. 1:18

    security vulnerability.

  29. 1:20

    It's beyond the scope of this video, but basically making a client-side request to

  30. 1:23

    a third-party website could allow an attacker to read what's in memory on the client's

  31. 1:28

    device.

  32. 1:29

    But we can easily overcome this limitation by creating a Cloud Function that will serve

  33. 1:31

    as a middleman.

  34. 1:32

    On our frontend application, the user might paste a couple of URLs into a form, then we'll

  35. 1:37

    send those URLs to our Cloud Function, which will then make the request to those third-party

  36. 1:41

    websites in an isolated environment.

  37. 1:43

    And this is where the web scraping comes in.

  38. 1:45

    Because the Cloud Function is going to retrieve an HTML document, which is just a big string

  39. 1:49

    of HTML, then it will process that HTML looking for meta tags that we can use for the link

  40. 1:54

    preview, things like the description, title, and featured image.

  41. 1:57

    So let's go ahead and jump into the code, and the first thing we'll do is initialize