fix price parsing
This commit is contained in:
@ -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)
|
||||||
} else {
|
// Cela transforme "49," en "49" et "99" reste "99"
|
||||||
System.out.println("Prix non trouvé");
|
String cleanPrice = priceElement.text().replaceAll("[^\\d]", "");
|
||||||
}
|
String cleanFraction = fractionElement.text().replaceAll("[^\\d]", "");
|
||||||
|
|
||||||
if(fractionElement != null) {
|
System.out.println("Prix brut détecté : " + cleanPrice + "," + cleanFraction);
|
||||||
String fraction = fractionElement.text();
|
|
||||||
}else{
|
|
||||||
System.out.println("Fraction non trouvé");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fractionElement != null && priceElement != null) {
|
float price = Float.parseFloat(cleanPrice);
|
||||||
|
float fraction = Float.parseFloat(cleanFraction);
|
||||||
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);
|
return price + (fraction / 100);
|
||||||
|
} else {
|
||||||
|
System.err.println("Éléments de prix manquants pour l'URL : " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user