You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

32 lines
1.1 KiB

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public abstract class AbstractCrawlStrategy<T> implements CrawlStrategy<T> {
@Override
public Document fetchDocument(String url) throws IOException {
return Jsoup.connect(url)
.timeout(15000)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
.get();
}
protected double parsePrice(String text) {
try {
String cleanText = text.replaceAll("[^0-9.]", "");
if (cleanText.isEmpty()) return 0.0;
return Double.parseDouble(cleanText);
} catch (Exception e) {
return 0.0;
}
}
protected double parseDiscount(double price, double originalPrice) {
if (originalPrice <= 0) return 10.0;
return Math.round((price / originalPrice) * 100) / 10.0;
}
}