Add unit price and unit parsing for items
This update enhances the `Item` structure to include `UnitPrice` and `Unit` fields. Additional logic is implemented to extract and parse unit pricing details from the HTML, improving data accuracy and granularity.
This commit is contained in:
parent
6de455b1bd
commit
81ea656332
@ -22,9 +22,11 @@ var ErrNilURL = errors.New("url is nil")
|
|||||||
var ErrInvalidURL = errors.New("invalid url")
|
var ErrInvalidURL = errors.New("invalid url")
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID int
|
ID int
|
||||||
Name string
|
Name string
|
||||||
Price float64
|
Price float64
|
||||||
|
UnitPrice float64
|
||||||
|
Unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
func deferClose(c io.Closer) {
|
func deferClose(c io.Closer) {
|
||||||
@ -93,5 +95,23 @@ func (c Config) GetItemPrice(ctx context.Context, b extractor.Browser, u *url.UR
|
|||||||
res.Price = price
|
res.Price = price
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unitPrices := doc.Select(`span[data-test="per-unit-price"]`)
|
||||||
|
|
||||||
|
if len(unitPrices) != 0 {
|
||||||
|
unitPriceStr, _ := unitPrices[0].Text()
|
||||||
|
unitPriceStr = strings.TrimSpace(unitPriceStr)
|
||||||
|
unitPriceStr = strings.ReplaceAll(unitPriceStr, "(", "")
|
||||||
|
unitPriceStr = strings.ReplaceAll(unitPriceStr, ")", "")
|
||||||
|
unitPriceStr = strings.ReplaceAll(unitPriceStr, "$", "")
|
||||||
|
unitPriceStr = strings.ReplaceAll(unitPriceStr, ",", "")
|
||||||
|
|
||||||
|
units := strings.Split(unitPriceStr, "/")
|
||||||
|
|
||||||
|
if len(units) > 1 {
|
||||||
|
res.Unit = strings.TrimSpace(units[1])
|
||||||
|
res.UnitPrice, _ = strconv.ParseFloat(units[0], 64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user