Skip to content

Commit 388f5ac

Browse files
honzajavorekdaveomri
authored andcommitted
style: use dollar variables (locating elements) (apify#1841)
As I progressed with apify#1584 I felt the code examples were starting to be more and more complex. Then I remembered that when I was young, us jQuery folks used to lean towards a naming convention where variables holding jQuery selections were prefixed with $. I changed the code examples in all lessons to adhere to this as I feel it makes them more readable and less cluttered. ----- ℹ️ The changes still use `$.map` and `$.each`, because they were made prior to the facb3c0 commit. It's gonna happen, but not yet.
1 parent 2d40cc9 commit 388f5ac

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

sources/academy/webscraping/scraping_basics_javascript2/06_locating_elements.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ if (response.ok) {
8080
const $ = cheerio.load(html);
8181

8282
$(".product-item").each((i, element) => {
83-
const productItem = $(element);
83+
const $productItem = $(element);
8484

85-
const title = productItem.find(".product-item__title");
86-
const titleText = title.text();
85+
const $title = $productItem.find(".product-item__title");
86+
const title = $title.text();
8787

88-
const price = productItem.find(".price");
89-
const priceText = price.text();
88+
const $price = $productItem.find(".price");
89+
const price = $price.text();
9090

91-
console.log(`${titleText} | ${priceText}`);
91+
console.log(`${title} | ${price}`);
9292
});
9393
} else {
9494
throw new Error(`HTTP ${response.status}`);
@@ -108,6 +108,12 @@ Sony XBR-950G BRAVIA 4K HDR Ultra HD TV |
108108

109109
There's still some room for improvement, but it's already much better!
110110

111+
:::info Dollar sign variable names
112+
113+
In jQuery and Cheerio, the core idea is a collection that wraps selected objects, usually HTML elements. To tell these wrapped selections apart from plain arrays, strings or other objects, it's common to start variable names with a dollar sign. This is just a naming convention to improve readability. The dollar sign has no special meaning and works like any other character in a variable name.
114+
115+
:::
116+
111117
## Precisely locating price
112118

113119
In the output we can see that the price isn't located precisely. For each product, our scraper also prints the text `Sale price`. Let's look at the HTML structure again. Each bit containing the price looks like this:
@@ -170,16 +176,16 @@ if (response.ok) {
170176
const $ = cheerio.load(html);
171177

172178
$(".product-item").each((i, element) => {
173-
const productItem = $(element);
179+
const $productItem = $(element);
174180

175-
const title = productItem.find(".product-item__title");
176-
const titleText = title.text();
181+
const $title = $productItem.find(".product-item__title");
182+
const title = $title.text();
177183

178184
// highlight-next-line
179-
const price = productItem.find(".price").contents().last();
180-
const priceText = price.text();
185+
const $price = $productItem.find(".price").contents().last();
186+
const price = $price.text();
181187

182-
console.log(`${titleText} | ${priceText}`);
188+
console.log(`${title} | ${price}`);
183189
});
184190
} else {
185191
throw new Error(`HTTP ${response.status}`);
@@ -243,18 +249,17 @@ Djibouti
243249
const $ = cheerio.load(html);
244250

245251
$(".wikitable").each((i, tableElement) => {
246-
const table = $(tableElement);
247-
const rows = table.find("tr");
248-
249-
rows.each((j, rowElement) => {
250-
const row = $(rowElement);
251-
const cells = row.find("td");
252-
253-
if (cells.length > 0) {
254-
const thirdColumn = $(cells[2]);
255-
const link = thirdColumn.find("a").first();
256-
const linkText = link.text();
257-
console.log(linkText);
252+
const $table = $(tableElement);
253+
const $rows = $table.find("tr");
254+
255+
$rows.each((j, rowElement) => {
256+
const $row = $(rowElement);
257+
const $cells = $row.find("td");
258+
259+
if ($cells.length > 0) {
260+
const $thirdColumn = $($cells[2]);
261+
const $link = $thirdColumn.find("a").first();
262+
console.log($link.text());
258263
}
259264
});
260265
});
@@ -289,10 +294,9 @@ Simplify the code from previous exercise. Use a single for loop and a single CSS
289294
const $ = cheerio.load(html);
290295

291296
$(".wikitable tr td:nth-child(3)").each((i, element) => {
292-
const nameCell = $(element);
293-
const link = nameCell.find("a").first();
294-
const linkText = link.text();
295-
console.log(linkText);
297+
const $nameCell = $(element);
298+
const $link = $nameCell.find("a").first();
299+
console.log($link.text());
296300
});
297301
} else {
298302
throw new Error(`HTTP ${response.status}`);

0 commit comments

Comments
 (0)