You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -197,110 +197,121 @@ Perhaps surprisingly, some products with variants will have the price field set.
197
197
198
198
## Parsing price
199
199
200
-
The items now contain the variant as text, which is good for a start, but we want the price to be in the `price`key. Let's introduce a new function to handle that:
200
+
The items now contain the variant as text, which is good for a start, but we want the price to be in the `price`property. Let's introduce a new function to handle that:
201
201
202
-
```py
203
-
defparse_variant(variant):
204
-
text = variant.text.strip()
205
-
name, price_text = text.split(" - ")
206
-
price = Decimal(
207
-
price_text
208
-
.replace("$", "")
209
-
.replace(",", "")
210
-
)
211
-
return {"variant_name": name, "price": price}
202
+
```js
203
+
functionparseVariant($option) {
204
+
const [variantName, priceText] = $option
205
+
.text()
206
+
.trim()
207
+
.split(" - ");
208
+
constprice=parseInt(
209
+
priceText
210
+
.replace("$", "")
211
+
.replace(".", "")
212
+
.replace(",", "")
213
+
);
214
+
return { variantName, price };
215
+
}
212
216
```
213
217
214
-
First, we split the text into two parts, then we parse the price as a decimal number. This part is similar to what we already do for parsing product listing prices. The function returns a dictionary we can merge with `item`.
218
+
First, we split the text into two parts, then we parse the price as a number. This part is similar to what we already do for parsing product listing prices. The function returns an object we can merge with `item`.
215
219
216
220
## Saving price
217
221
218
222
Now, if we use our new function, we should finally get a program that can scrape exact prices for all products, even if they have variants. The whole code should look like this now:
"title": "Sony XB-950B1 Extra Bass Wireless Headphones with App Control",
334
+
"minPrice": 12800,
335
+
"price": 17800,
336
+
"vendor": "Sony",
337
+
"variantName": "Black"
327
338
},
328
339
...
329
340
]
330
341
```
331
342
332
-
Success! We managed to build a Python application for watching prices!
343
+
Success! We managed to build a Node.js application for watching prices!
333
344
334
345
Is this the end? Maybe! In the next lesson, we'll use a scraping framework to build the same application, but with less code, faster requests, and better visibility into what's happening while we wait for the program to finish.
0 commit comments