diff --git a/sources/academy/webscraping/scraping_basics_javascript2/04_downloading_html.md b/sources/academy/webscraping/scraping_basics_javascript2/04_downloading_html.md index ec361214f..0fd340722 100644 --- a/sources/academy/webscraping/scraping_basics_javascript2/04_downloading_html.md +++ b/sources/academy/webscraping/scraping_basics_javascript2/04_downloading_html.md @@ -12,61 +12,101 @@ import Exercises from './_exercises.mdx'; --- -Using browser tools for developers is crucial for understanding the structure of a particular page, but it's a manual task. Let's start building our first automation, a Python program which downloads HTML code of the product listing. +Using browser tools for developers is crucial for understanding the structure of a particular page, but it's a manual task. Let's start building our first automation, a JavaScript program which downloads HTML code of the product listing. -## Starting a Python project +## Starting a Node.js project -Before we start coding, we need to set up a Python project. Let's create new directory with a virtual environment. Inside the directory and with the environment activated, we'll install the HTTPX library: +Before we start coding, we need to set up a Node.js project. Let's create new directory and let's name it `product-scraper`. Inside the directory, we'll initialize new project: ```text -$ pip install httpx +$ npm init +This utility will walk you through creating a package.json file. ... -Successfully installed ... httpx-0.0.0 -``` -:::tip Installing packages +Press ^C at any time to quit. +package name: (product-scraper) +version: (1.0.0) +description: Product scraper +entry point: (index.js) +test command: +git repository: +keywords: +author: +license: (ISC) +# highlight-next-line +type: (commonjs) module +About to write to /Users/.../product-scraper/package.json: + +{ + "name": "product-scraper", + "version": "1.0.0", + "description": "Product scraper", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", +# highlight-next-line + "type": "module" +} +``` -Being comfortable around Python project setup and installing packages is a prerequisite of this course, but if you wouldn't say no to a recap, we recommend the [Installing Packages](https://packaging.python.org/en/latest/tutorials/installing-packages/) tutorial from the official Python Packaging User Guide. +The above creates a `package.json` file with configuration of our project. While most of the values are arbitrary, it's important that the project's type is set to `module`. Now let's test that all works. Inside the project directory we'll create a new file called `index.js` with the following code: -::: +```js +import process from 'node:process'; -Now let's test that all works. Inside the project directory we'll create a new file called `main.py` with the following code: +console.log(`All is OK, ${process.argv[2]}`); +``` -```py -import httpx +Running it as a Node.js program will verify that our setup is okay and we've correctly set the type to `module`. The program takes a single word as an argument and will address us with it, so let's pass it "mate", for example: -print("OK") +```text +$ node index.js mate +All is OK, mate ``` -Running it as a Python program will verify that our setup is okay and we've installed HTTPX: +:::info Troubleshooting + +If you see errors or are otherwise unable to run the code above, it likely means your environment isn't set up correctly. Unfortunately, diagnosing the issue is out of scope for this course. + +Make sure that in your `package.json` the type property is set to `module`, otherwise you'll get the following warning: ```text -$ python main.py -OK +[MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///Users/.../product-scraper/index.js is not specified and it doesn't parse as CommonJS. +Reparsing as ES module because module syntax was detected. This incurs a performance overhead. +To eliminate this warning, add "type": "module" to /Users/.../product-scraper/package.json. ``` -:::info Troubleshooting +In older versions of Node.js, you may even encounter this error: -If you see errors or for any other reason cannot run the code above, it means that your environment isn't set up correctly. We're sorry, but figuring out the issue is out of scope of this course. +```text +SyntaxError: Cannot use import statement outside a module +``` ::: ## Downloading product listing -Now onto coding! Let's change our code so it downloads HTML of the product listing instead of printing `OK`. The [documentation of the HTTPX library](https://www.python-httpx.org/) provides us with examples how to use it. Inspired by those, our code will look like this: - -```py -import httpx +Now onto coding! Let's change our code so it downloads HTML of the product listing instead of printing `All is OK`. The [documentation of the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) provides us with examples how to use it. Inspired by those, our code will look like this: -url = "https://warehouse-theme-metal.myshopify.com/collections/sales" -response = httpx.get(url) -print(response.text) +```js +const url = "https://warehouse-theme-metal.myshopify.com/collections/sales"; +const response = await fetch(url); +console.log(await response.text()); ``` +:::tip Asynchronous flow + +First time you see `await`? It's a modern syntax for working with promises. See the [JavaScript Asynchronous Programming and Callbacks](https://nodejs.org/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks) and [Discover Promises in Node.js](https://nodejs.org/en/learn/asynchronous-work/discover-promises-in-nodejs) tutorials in the official Node.js documentation for more. + +::: + If we run the program now, it should print the downloaded HTML: ```text -$ python main.py +$ node index.js
@@ -80,7 +120,7 @@ $ python main.py ``` -Running `httpx.get(url)`, we made a HTTP request and received a response. It's not particularly useful yet, but it's a good start of our scraper. +Running `await fetch(url)`, we made a HTTP request and received a response. It's not particularly useful yet, but it's a good start of our scraper. :::tip Client and server, request and response @@ -88,7 +128,7 @@ HTTP is a network protocol powering the internet. Understanding it well is an im - HTTP is an exchange between two participants. - The _client_ sends a _request_ to the _server_, which replies with a _response_. -- In our case, `main.py` is the client, and the technology running at `warehouse-theme-metal.myshopify.com` replies to our request as the server. +- In our case, `index.js` is the client, and the technology running at `warehouse-theme-metal.myshopify.com` replies to our request as the server. ::: @@ -110,28 +150,30 @@ First, let's ask for trouble. We'll change the URL in our code to a page that do https://warehouse-theme-metal.myshopify.com/does/not/exist ``` -We could check the value of `response.status_code` against a list of allowed numbers, but HTTPX already provides `response.raise_for_status()`, a method that analyzes the number and raises the `httpx.HTTPError` exception if our request wasn't successful: +We could check the value of `response.status` against a list of allowed numbers, but the Fetch API already provides `response.ok`, a property which returns `false` if our request wasn't successful: -```py -import httpx +```js +const url = "https://warehouse-theme-metal.myshopify.com/does/not/exist"; +const response = await fetch(url); -url = "https://warehouse-theme-metal.myshopify.com/does/not/exist" -response = httpx.get(url) -response.raise_for_status() -print(response.text) +if (response.ok) { + console.log(await response.text()); +} else { + throw new Error(`HTTP ${response.status}`); +} ``` If you run the code above, the program should crash: ```text -$ python main.py -Traceback (most recent call last): - File "/Users/.../main.py", line 5, in