Skip to content

Commit e8a177b

Browse files
committed
style: use dollar variables (locating elements)
1 parent 3bbb503 commit e8a177b

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

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

Lines changed: 26 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}`);
@@ -170,16 +170,16 @@ if (response.ok) {
170170
const $ = cheerio.load(html);
171171

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

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

178178
// highlight-next-line
179-
const price = productItem.find(".price").contents().last();
180-
const priceText = price.text();
179+
const $price = $productItem.find(".price").contents().last();
180+
const price = $price.text();
181181

182-
console.log(`${titleText} | ${priceText}`);
182+
console.log(`${title} | ${price}`);
183183
});
184184
} else {
185185
throw new Error(`HTTP ${response.status}`);
@@ -243,18 +243,17 @@ Djibouti
243243
const $ = cheerio.load(html);
244244

245245
$(".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);
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+
console.log($link.text());
258257
}
259258
});
260259
});
@@ -289,10 +288,9 @@ Simplify the code from previous exercise. Use a single for loop and a single CSS
289288
const $ = cheerio.load(html);
290289

291290
$(".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);
291+
const $nameCell = $(element);
292+
const $link = $nameCell.find("a").first();
293+
console.log($link.text());
296294
});
297295
} else {
298296
throw new Error(`HTTP ${response.status}`);

0 commit comments

Comments
 (0)