继续优化代码
This commit is contained in:
@@ -92,6 +92,47 @@ public class MoneyScraper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("testScraperGetMoneyControlStockNoScId")
|
||||||
|
public String testScraperGetMoneyControlStockNoScId() {
|
||||||
|
String url = "http://www.moneycontrol.com/india/stockpricequote/foodprocessing/nestleindia/NI";
|
||||||
|
/* Document soup2 = fetchCompanyDetails(url);
|
||||||
|
|
||||||
|
if (soup2 != null) {
|
||||||
|
Element comIdInput = soup2.selectFirst("input[id=ap_sc_id]");
|
||||||
|
String companyCodeId = null;
|
||||||
|
if (comIdInput != null) {
|
||||||
|
companyCodeId = comIdInput.val();
|
||||||
|
log.info(Thread.currentThread().getName() + ",the stock url: " + url + ", THE input id: " + companyCodeId);
|
||||||
|
} else {
|
||||||
|
log.error(Thread.currentThread().getName() + " No <input> with id='ap_sc_id' found on the website.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (soup2 != null) {
|
||||||
|
Element ulElement = soup2.selectFirst("ul[id=nseBseTab]");
|
||||||
|
|
||||||
|
if (ulElement != null) {
|
||||||
|
for (Element aElement : ulElement.select("a")) {
|
||||||
|
String exchangeValue = aElement.text().trim();
|
||||||
|
if ("BSE".equals(exchangeValue) || "NSE".equals(exchangeValue)) {
|
||||||
|
log.info(Thread.currentThread().getName() + ",the stock url: " + url +
|
||||||
|
", the exchange Value: " + exchangeValue);
|
||||||
|
MoneyStock build = MoneyStock.builder().stockName("sss").stockType(exchangeValue.toLowerCase(Locale.ROOT))
|
||||||
|
.detailUrl(String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s", exchangeValue.toLowerCase(), companyCodeId))
|
||||||
|
.selfUrl(url)
|
||||||
|
.selfDispId(extractDispId(url))
|
||||||
|
.moneyScId(companyCodeId).saveTime(new Date()).build();
|
||||||
|
List<MoneyStock> all = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.stockName.eq("Nestle"), QMoneyStockPO.moneyStockPO.stockType.eq(exchangeValue.toLowerCase(Locale.ROOT)));
|
||||||
|
if (CollectionUtil.isEmpty(all)) {
|
||||||
|
moneyStockRepository.save(build);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 带有A B 分组的url
|
* 带有A B 分组的url
|
||||||
@@ -132,8 +173,7 @@ public class MoneyScraper {
|
|||||||
break; // 如果成功则跳出循环
|
break; // 如果成功则跳出循环
|
||||||
} catch (IOException | RuntimeException e) {
|
} catch (IOException | RuntimeException e) {
|
||||||
// 处理异常的逻辑
|
// 处理异常的逻辑
|
||||||
e.printStackTrace();
|
log.error("Attempt " + attempt + " failed. Retrying...",e);
|
||||||
log.error("Attempt " + attempt + " failed. Retrying...");
|
|
||||||
attempt++;
|
attempt++;
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000); // 休眠1秒
|
Thread.sleep(1000); // 休眠1秒
|
||||||
@@ -207,7 +247,7 @@ public class MoneyScraper {
|
|||||||
|
|
||||||
if (soup2 != null) {
|
if (soup2 != null) {
|
||||||
Element comIdInput = soup2.selectFirst("input[id=ap_sc_id]");
|
Element comIdInput = soup2.selectFirst("input[id=ap_sc_id]");
|
||||||
String companyCodeId = "";
|
String companyCodeId = null;
|
||||||
if (comIdInput != null) {
|
if (comIdInput != null) {
|
||||||
companyCodeId = comIdInput.val();
|
companyCodeId = comIdInput.val();
|
||||||
log.info(Thread.currentThread().getName()+",the stockName: " + textContent + ", THE input id: " + companyCodeId);
|
log.info(Thread.currentThread().getName()+",the stockName: " + textContent + ", THE input id: " + companyCodeId);
|
||||||
|
|||||||
Reference in New Issue
Block a user