|
|
@@ -9,7 +9,6 @@ import com.jzg.quotation.zijin.crawler.entity.response.*;
|
|
|
import com.jzg.quotation.zijin.crawler.enums.RequestUrl;
|
|
|
import com.jzg.quotation.zijin.crawler.properties.ZiJinCrawlerProperties;
|
|
|
import com.jzg.quotation.zijin.crawler.util.DESEncryption;
|
|
|
-import com.jzg.quotation.zijin.crawler.util.RS6Item;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
@@ -19,6 +18,7 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -39,13 +39,15 @@ public class ZiJinCrawlerRequestComponent {
|
|
|
// 循环次数
|
|
|
public static final int CIRCULATE_NUMBER = 3;
|
|
|
|
|
|
-
|
|
|
private final RequestComponent requestComponent;
|
|
|
|
|
|
private final ZiJinCrawlerProperties ziJinCrawlerProperties;
|
|
|
|
|
|
private final ExteriorUtilsRequest exteriorUtilsRequest;
|
|
|
|
|
|
+ // 用于调用Python外放接口获取Cookie
|
|
|
+ private final RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
/**
|
|
|
* 获取验证码
|
|
|
*
|
|
|
@@ -513,15 +515,39 @@ public class ZiJinCrawlerRequestComponent {
|
|
|
|
|
|
/**
|
|
|
* 获取Cookie的值
|
|
|
+ * 通过调用Python外放接口获取Cookie
|
|
|
*
|
|
|
- * @return String
|
|
|
- * @throws IOException
|
|
|
+ * @return String Cookie字符串
|
|
|
+ * @throws IOException 如果获取Cookie时发生错误
|
|
|
*/
|
|
|
- public static String getCookie() throws IOException {
|
|
|
- RS6Item rs6Item = RS6Item.createRS6Instance();
|
|
|
- rs6Item.updata_js_rs6();
|
|
|
- Map<String, String> cookies = rs6Item.updateCookies();
|
|
|
- return rs6Item.convertJsonCookieToSemicolonFormat(cookies.toString());
|
|
|
+ public String getCookie() throws IOException {
|
|
|
+ String cookieUrl = ziJinCrawlerProperties.getCookiePath();
|
|
|
+ if (cookieUrl == null || cookieUrl.trim().isEmpty()) {
|
|
|
+ throw new IOException("紫金crawler.cookie-path未配置");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ResponseEntity<String> response = restTemplate.getForEntity(cookieUrl, String.class);
|
|
|
+ String body = response.getBody();
|
|
|
+ if (body == null || body.trim().isEmpty()) {
|
|
|
+ throw new IOException("获取Cookie失败: Python接口返回空内容");
|
|
|
+ }
|
|
|
+ // 解析JSON响应,提取cookie字段
|
|
|
+ com.alibaba.fastjson2.JSONObject jsonObject = JSON.parseObject(body);
|
|
|
+ if (!jsonObject.getBooleanValue("ok")) {
|
|
|
+ throw new IOException("获取Cookie失败: Python接口返回ok=false");
|
|
|
+ }
|
|
|
+ String cookie = jsonObject.getString("cookie");
|
|
|
+ if (cookie == null || cookie.trim().isEmpty()) {
|
|
|
+ throw new IOException("获取Cookie失败: 响应中cookie字段为空");
|
|
|
+ }
|
|
|
+ log.info("紫金Cookie获取成功");
|
|
|
+ return cookie.trim();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用Python接口获取Cookie失败: {}", e.getMessage(), e);
|
|
|
+ throw new IOException("获取Cookie失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|