Refine selectors and enhance price parsing with logging

Adjusted HTML selectors for improved compatibility and updated price parsing logic to handle additional formats. Added logging to provide better debugging insights during price extraction.
This commit is contained in:
2025-10-10 14:42:01 -04:00
parent dc43d1626a
commit 9947cae947
2 changed files with 11 additions and 5 deletions

View File

@@ -62,13 +62,13 @@ func (c Config) GetItemFromURL(ctx context.Context, b extractor.Browser, u *url.
return res, fmt.Errorf("failed to open page: %w", err)
}
names := doc.Select("h2.h4")
names := doc.Select(".h4")
if len(names) > 0 {
res.Name, _ = names[0].Text()
}
prices := doc.Select("h4.h2")
prices := doc.Select(".h2")
if len(prices) > 0 {
priceStr, _ := prices[0].Text()

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"log/slog"
"net/url"
"strconv"
"strings"
@@ -80,23 +81,28 @@ func (c Config) GetItemPrice(ctx context.Context, b extractor.Browser, u *url.UR
ID: id,
}
titles := doc.Select("h1[data-test]")
titles := doc.Select("h1[data-testid]")
if len(titles) != 0 {
res.Name, _ = titles[0].Text()
}
prices := doc.Select("span[data-test=\"amount\"] span:nth-child(1)")
prices := doc.Select("div.component--product-price:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(2)")
slog.Info("prices", "len", len(prices))
if len(prices) != 0 {
priceStr, _ := prices[0].Text()
slog.Info("price", "0", prices[0], "text", priceStr)
priceStr = strings.ReplaceAll(priceStr, "$", "")
priceStr = strings.ReplaceAll(priceStr, ",", "")
// if there's a "/" in the price, then it's in the format of like "1.99/ea", so split it off
priceStr = strings.Split(priceStr, "/")[0]
price, _ := strconv.ParseFloat(priceStr, 64)
slog.Info("price", "0", prices[0], "text", priceStr, "price", price)
res.Price = price
}
unitPrices := doc.Select(`span[data-test="per-unit-price"]`)
unitPrices := doc.Select(`div.component--product-price:nth-child(1) span.price-per-unit`)
if len(unitPrices) != 0 {
unitPriceStr, _ := unitPrices[0].Text()