How to scrape HTML from a website built with Javascript?

How to scrape HTML from a website built with Javascript?

ยท

3 min read

Hello World โœŒ๏ธ,

In this article, I would like to tell about how you can scrape HTML content from a website build with the Javascript framework.


But why it is even a problem to scrape a JS-based website? ๐Ÿค”


Problem Definition:

You need to have a browser environment in order to execute Javascript code that will render HTML.


If you will try open this website (https://web-scraping-playground-site.firebaseapp.com) in your browser โ€” you will see a simple page with some content.


Website built with javascript framework

However, if you will try to send HTTP GET request to the same url in the Postman โ€” you will see a different response.


No HTML Response from a website build with javascript

A response to GET request โ€˜https://web-scraping-playground-site.firebaseapp.comโ€™ in made in the Postman.


What? Why the response contains no HTML? It is happening because there is no browser environment when we sending requests from a server or Postman app.



๐ŸŽ“ We need a browser environment for executing Javascript code and rendering content โ€” HTML.



It sounds like an easy and fun problem to solve! In the below ๐Ÿ‘‡ section I will show 2 ways how to solve the above-mentioned problem using:

  1. Puppeteer โ€” a Node library developed by Google.
  2. Proxybot โ€” an API service for web scraping.

Let's get started ๐Ÿ‘จโ€๐Ÿ’ป


For people who prefer watching videos, there is a quick video ๐ŸŽฅ demonstrating how to get an HTML content of a JS-based website.


Solution using Puppeteer

The idea is simple. Use puppeteer on our server for simulating the browser environment in order to render HTML of a page and use it for scraping or something else ๐Ÿ˜‰.

See the below code snippet.

This code simply:

  • Accepts GET request
  • Receives โ€˜urlโ€™ param
  • Returns response of the โ€˜getPageHTMLโ€™ function

The โ€˜getPageHTMLโ€™ function is the most interesting for us because thatโ€™s where the magic happens.

The โ€˜magicโ€™ is, however, pretty simple. The function simply does the following steps:

  • Launch puppeteer
  • Open the desired url
  • Internally executes JS
  • Extract HTML of the page
  • Return the HTML

Easy-peasy ๐Ÿ‘

Letโ€™s run the script and send a request to http://localhost:3000?url=https://web-scraping-playground-site.firebaseapp.com in the Postman app.

The below screenshot shows the response from our local server.


Using puppeteer to scrape website's HTML



Yaaaaay ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰ We Did it! Great job guys! We got HTML back!

It was easy, but it can be even easier, letโ€™s have a look at the second approach.



Solution using Proxybot

With this approach, we actually only need to send an HTTP GET request. The API service will run a virtual browser internally and send you back HTML.

https://proxybot.io/api/v1/API_KEY?render_js=true&url=your-url-here

Letโ€™s try to call the API in the Postman app.

Yaaay ๐ŸŽŠ๐ŸŽŠ๐ŸŽŠ More HTML!

There is not much to say about the request, because it is pretty straightforward. However, I want to emphasize a small detail. When calling the API to remember to include the render_js=true url param.

Otherwise, the service will not execute Javascript ๐Ÿค“



Congratulations ๐Ÿฅณ Now you can scrape websites build with javascript frameworks like Angular, React, Ember etc..

I hope this article was interesting and useful.

Proxybot it just one of the services allowing you to proxy your requests. If you are looking for proxy providers here you can find a list with best proxy providers.

ย