Upload files to "/"
This commit is contained in:
38
CurrencyParser.java
Normal file
38
CurrencyParser.java
Normal file
@@ -0,0 +1,38 @@
|
||||
import java.util.regex.*;
|
||||
|
||||
public class CurrencyParser {
|
||||
|
||||
/**
|
||||
* Парсит курс USD с актуальной страницы ЦБ
|
||||
* РФ[](https://cbr.ru/currency_base/daily/)
|
||||
* Структура: таблица с строкой
|
||||
* <tr>
|
||||
* <td>840</td>
|
||||
* <td>USD</td>...
|
||||
* <td>76,0535</td>
|
||||
* </tr>
|
||||
*/
|
||||
public static double parseUSD(String html) {
|
||||
|
||||
// Более точный и надёжный regex под текущую верстку сайта
|
||||
Pattern pattern = Pattern.compile(
|
||||
"<td>USD</td>\\s*" +
|
||||
"<td>1</td>\\s*" +
|
||||
"<td>Доллар США</td>\\s*" +
|
||||
"<td>([0-9,]+)</td>",
|
||||
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
||||
|
||||
Matcher matcher = pattern.matcher(html);
|
||||
|
||||
if (matcher.find()) {
|
||||
String rateStr = matcher.group(1).replace(',', '.'); // заменяем запятую на точку
|
||||
try {
|
||||
return Double.parseDouble(rateStr);
|
||||
} catch (NumberFormatException e) {
|
||||
System.err.println("Ошибка парсинга числа: " + matcher.group(1));
|
||||
}
|
||||
}
|
||||
|
||||
return -1; // не удалось найти
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user