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:
@@ -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()
|
||||
|
Reference in New Issue
Block a user