fix price parsing

This commit is contained in:
Σlie *
2026-02-27 20:07:48 +01:00
parent 595f2bce3b
commit c5575e5441

View File

@ -16,50 +16,36 @@ public class WebScrapper {
instance = this; instance = this;
} }
public float requestPrice(String url) public float requestPrice(String url) {
{
try { try {
// IMPORTANT : Amazon bloque les requêtes sans "User-Agent"
Document doc = Jsoup.connect(url) Document doc = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.timeout(10000)
.get(); .get();
// On cherche la classe .a-price-whole
Element priceElement = doc.selectFirst(".a-price-whole"); Element priceElement = doc.selectFirst(".a-price-whole");
Element fractionElement = doc.selectFirst(".a-price-fraction"); Element fractionElement = doc.selectFirst(".a-price-fraction");
if (priceElement != null) { if (priceElement != null && fractionElement != null) {
String price = priceElement.text(); // NETTOYAGE : On ne garde que les chiffres (\d)
// Cela transforme "49," en "49" et "99" reste "99"
String cleanPrice = priceElement.text().replaceAll("[^\\d]", "");
String cleanFraction = fractionElement.text().replaceAll("[^\\d]", "");
System.out.println("Prix brut détecté : " + cleanPrice + "," + cleanFraction);
float price = Float.parseFloat(cleanPrice);
float fraction = Float.parseFloat(cleanFraction);
return price + (fraction / 100);
} else { } else {
System.out.println("Prix non trouvé"); System.err.println("Éléments de prix manquants pour l'URL : " + url);
} }
if(fractionElement != null) {
String fraction = fractionElement.text();
}else{
System.out.println("Fraction non trouvé");
}
if(fractionElement != null && priceElement != null) {
System.out.println("Le prix est de: " + priceElement.text() + fractionElement.text());
float price = Float.parseFloat(priceElement.text());
float fraction = Float.parseFloat(fractionElement.text());
return price + (fraction / 100);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); System.err.println("Erreur lors du scraping du prix : " + e.getMessage());
} }
return 0.0f; return 0.0f;
} }
public String requestImage(String urlPage) { public String requestImage(String urlPage) {