fix price parsing
This commit is contained in:
@ -16,50 +16,36 @@ public class WebScrapper {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public float requestPrice(String url)
|
||||
{
|
||||
|
||||
public float requestPrice(String url) {
|
||||
try {
|
||||
// IMPORTANT : Amazon bloque les requêtes sans "User-Agent"
|
||||
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")
|
||||
.timeout(10000)
|
||||
.get();
|
||||
|
||||
// On cherche la classe .a-price-whole
|
||||
Element priceElement = doc.selectFirst(".a-price-whole");
|
||||
|
||||
Element fractionElement = doc.selectFirst(".a-price-fraction");
|
||||
|
||||
if (priceElement != null) {
|
||||
String price = priceElement.text();
|
||||
if (priceElement != null && fractionElement != null) {
|
||||
// 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 {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Erreur lors du scraping du prix : " + e.getMessage());
|
||||
}
|
||||
return 0.0f;
|
||||
|
||||
}
|
||||
|
||||
public String requestImage(String urlPage) {
|
||||
|
||||
Reference in New Issue
Block a user