package com.fox.spider.stock.api.tencent;
import com.fox.spider.stock.constant.StockConst;
import com.fox.spider.stock.entity.dto.http.HttpResponseDto;
import com.fox.spider.stock.entity.po.tencent.TencentRealtimeDealInfoPo;
import com.fox.spider.stock.entity.vo.StockVo;
import com.fox.spider.stock.util.BigDecimalUtil;
import com.fox.spider.stock.util.DateUtil;
import com.fox.spider.stock.util.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
/**
* 腾讯股票实时成交信息
*
* @author lusongsong
* @date 2020/12/24 13:38
*/
@Component
public class TencentRealtimeDealInfoApi extends TencentBaseApi {
/**
* 日志
*/
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 接口地址
* https://web.sqt.gtimg.cn/q=sh603383,sh601519
*/
private static final String API_URL = "https://web.sqt.gtimg.cn/q=";
/**
* 股票代码拼接字符串
*/
private static String STOCK_CODE_SPLIT_STR = ",";
/**
* 返回数据分割符
*/
private static String RESPONSE_SPLIT_STR = ";";
/**
* 交易数据分割符
*/
private static String DEAL_INFO_SPLIT_STR = "~";
/**
* 交易数据起止符
*/
private static String DEAL_INFO_STR = "\"";
/**
* 获取股票的实时交易数据
*
* @param stockVo
* @return
*/
public TencentRealtimeDealInfoPo realtimeDealInfo(StockVo stockVo) {
if (null == stockVo || null == stockVo.getStockCode() || null == stockVo.getStockMarket()) {
return null;
}
List<StockVo> stockVoList = Arrays.asList(stockVo);
Map<String, TencentRealtimeDealInfoPo> tencentRealtimeDealInfoPoMap = this.batchRealtimeDealInfo(stockVoList);
if (null != tencentRealtimeDealInfoPoMap && tencentRealtimeDealInfoPoMap.containsKey(stockVo.getStockCode())) {
return tencentRealtimeDealInfoPoMap.get(stockVo.getStockCode());
}
return null;
}
/**
* 批量获取股票的实时交易数据
*
* @param stockVoList
* @return
*/
public Map<String, TencentRealtimeDealInfoPo> batchRealtimeDealInfo(List<StockVo> stockVoList) {
if (null == stockVoList || stockVoList.isEmpty()) {
return null;
}
try {
List<String> tencnetStockCodeList = new ArrayList<>();
for (StockVo stockVo : stockVoList) {
String tencnetStockCode = TencentBaseApi.tencentStockCode(stockVo);
if (null != tencnetStockCode && !tencnetStockCode.isEmpty()) {
tencnetStockCodeList.add(tencnetStockCode);
}
}
if (tencnetStockCodeList.isEmpty()) {
return null;
}
HttpUtil httpUtil = new HttpUtil();
httpUtil.setUrl(API_URL + StringUtils.join(tencnetStockCodeList, STOCK_CODE_SPLIT_STR))
.setOriCharset(HttpUtil.CHARSET_GBK)
.setErrorOriCharset(HttpUtil.CHARSET_UTF8);
HttpResponseDto httpResponse = httpUtil.request();
return this.handleResponse(httpResponse.getContent());
} catch (IOException e) {
logger.error("batchRealtimeDealInfo", e);
}
return null;
}
/**
* 处理接口返回数据
*
* @param response
* @return
*/
private Map<String, TencentRealtimeDealInfoPo> handleResponse(String response) {
if (null != response && !response.isEmpty() && response.contains(RESPONSE_SPLIT_STR)) {
String[] responseArr = response.trim().split(RESPONSE_SPLIT_STR);
HashMap<String, TencentRealtimeDealInfoPo> tencentRealtimeDealInfoPoMap = new HashMap<>(responseArr.length);
for (int i = 0; i < responseArr.length; i++) {
String singleResponse = responseArr[i];
if (null == singleResponse || singleResponse.isEmpty() || !response.contains(DEAL_INFO_SPLIT_STR)) {
continue;
}
String stockCodeStr = getStockCodeStr(singleResponse);
StockVo stockVo = tencnetStockCodeToStockVo(stockCodeStr);
int startIndex = singleResponse.indexOf(DEAL_INFO_STR);
int endIndex = singleResponse.lastIndexOf(DEAL_INFO_STR);
if (startIndex > 0 && endIndex > 0) {
singleResponse = singleResponse.substring(startIndex + 1, endIndex);
String[] dealInfoArr = singleResponse.split(DEAL_INFO_SPLIT_STR);
TencentRealtimeDealInfoPo tencentRealtimeDealInfoPo = getDealInfo(stockVo, dealInfoArr);
if (null == tencentRealtimeDealInfoPo) {
continue;
}
tencentRealtimeDealInfoPo.setStockMarket(stockVo.getStockMarket());
tencentRealtimeDealInfoPoMap.put(stockVo.getStockCode(), tencentRealtimeDealInfoPo);
}
}
return tencentRealtimeDealInfoPoMap;
}
return null;
}
/**
* 获取腾讯股票代码
*
* @param response
* @return
*/
private static String getStockCodeStr(String response) {
response = response.trim();
int index = response.lastIndexOf("=");
response = response.substring(0, index);
response = response.replace("v_", "");
return response;
}
/**
* 获取实时交易信息
*
* @param stockVo
* @param dealInfoArr
* @return
*/
public static TencentRealtimeDealInfoPo getDealInfo(StockVo stockVo, String[] dealInfoArr) {
if (null == stockVo || null == dealInfoArr) {
return null;
}
switch (stockVo.getStockMarket()) {
case StockConst.SM_SH:
case StockConst.SM_SZ:
return getADealInfo(stockVo, dealInfoArr);
case StockConst.SM_HK:
return getHKDealInfo(stockVo, dealInfoArr);
default:
return null;
}
}
/**
* 获取A股交易信息
*
* @param stockVo
* @param dealInfoArr
* @return
*/
private static TencentRealtimeDealInfoPo getADealInfo(StockVo stockVo, String[] dealInfoArr) {
if (null == dealInfoArr) {
return null;
}
TencentRealtimeDealInfoPo tencentRealtimeDealInfoPo = new TencentRealtimeDealInfoPo();
List<String> unknownList = new ArrayList<>();
//售价
BigDecimal price = null;
//售量
Long num = null;
TreeMap<BigDecimal, Long> priceMap = new TreeMap();
for (int i = 1; i < dealInfoArr.length; i++) {
String dealInfo = dealInfoArr[i];
if (null == dealInfo || dealInfo.isEmpty()) {
continue;
}
if (1 == i) {
tencentRealtimeDealInfoPo.setStockName(dealInfo);
} else if (2 == i) {
tencentRealtimeDealInfoPo.setStockCode(dealInfo);
} else if (3 == i) {
tencentRealtimeDealInfoPo.setCurrentPrice(BigDecimalUtil.initPrice(dealInfo));
} else if (4 == i) {
tencentRealtimeDealInfoPo.setPreClosePrice(BigDecimalUtil.initPrice(dealInfo));
} else if (5 == i) {
tencentRealtimeDealInfoPo.setOpenPrice(BigDecimalUtil.initPrice(dealInfo));
} else if (6 == i || 36 == i) {
//手
tencentRealtimeDealInfoPo.setDealNum(handleDealNum(stockVo, dealInfo));
} else if (9 <= i && 28 >= i) {
if (0 == i % 2) {
num = handleDealNum(stockVo, dealInfo);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
资源推荐
资源详情
资源评论
收起资源包目录
股票相关爬虫接口.zip (130个子文件)
mvnw.cmd 6KB
.gitignore 414B
TencentRealtimeDealInfoApi.java 17KB
SinaBigDealApi.java 17KB
StockConst.java 16KB
SinaRealtimeDealInfoApi.java 16KB
IFengRealtimeDealInfoApi.java 15KB
HttpUtil.java 13KB
SZRealtimeDealApi.java 11KB
TencentKLineApi.java 10KB
NetsRealtimeDealInfoApi.java 9KB
TencentMinuteScopeKLineApi.java 8KB
DateUtil.java 8KB
HKKLineApi.java 8KB
IFengFiveDayMinuteKLineApi.java 8KB
TencentRealtimePriceDealNumApi.java 8KB
NetsFQKLineDataApi.java 8KB
SHRealtimeDealInfoApi.java 7KB
HKStockInfoApi.java 7KB
SHRealtimeMinuteKLineApi.java 7KB
TencentBigDealApi.java 7KB
SHStockInfoApi.java 7KB
SZKLineApi.java 7KB
TencentFiveDayMinuteKLineApi.java 7KB
NetsDayDealInfoApi.java 7KB
TencentRealtimeDealDetailApi.java 6KB
TencentRealtimeMinuteKLineApi.java 6KB
IFengRealtimeDealDetailApi.java 6KB
SinaRealtimeDealDetailApi.java 6KB
SinaRealtimeDealDetailTimeScopeApi.java 6KB
IFengFiveDayMinuteScopeKLineApi.java 6KB
NetsRealtimeMinuteKLineApi.java 6KB
IFengKLineApi.java 6KB
StockMarketStatusConst.java 5KB
IFengRealtimeBigDealApi.java 5KB
SZStockInfoApi.java 5KB
TencentRealtimeDealDetailTimeScopeApi.java 5KB
TencentKLineBaseApi.java 4KB
IFengRealtimePriceDealNumApi.java 4KB
SinaMinuteKLineDataApi.java 4KB
NetsFQTotalClosePriceApi.java 4KB
TencentApiTests.java 4KB
TencentRelateStockApi.java 4KB
TencentBaseApi.java 4KB
SinaRealtimePriceDealNumRatioApi.java 4KB
SinaPriceDealNumRatioApi.java 4KB
SinaFQPriceLineApi.java 4KB
TencentRelateBlockApi.java 4KB
SinaApiTests.java 4KB
StockToolImpl.java 3KB
NetsBaseApi.java 3KB
SinaRealtimeDealInfoPo.java 3KB
SinaBaseApi.java 3KB
TencentRealtimeDealInfoPo.java 3KB
IFengApiTests.java 2KB
IFengBaseApi.java 2KB
NetsApiTests.java 2KB
BigDecimalUtil.java 2KB
HttpResponseDto.java 2KB
SHRealtimeDealInfoPo.java 2KB
SHStockInfoPo.java 2KB
SZRealtimeDealPo.java 2KB
IFengRealtimeDealInfoPo.java 1KB
NetsRealtimeDealInfoPo.java 1KB
SHApiTests.java 1KB
SZApiTests.java 1KB
SinaBigDealSumPo.java 1KB
SZStockInfoPo.java 1KB
CharsetUtil.java 1KB
HKStockInfoPo.java 1KB
HKApiTests.java 1KB
NetsDayDealInfoPo.java 1KB
TencentRealtimeMinuteKLinePo.java 949B
JSONUtil.java 932B
SHRealtimeMinuteKLinePo.java 904B
StockToolServiceTests.java 903B
StockBaseApi.java 890B
SZKLineNodeDataPo.java 858B
NetsRealtimeMinuteKLinePo.java 840B
TencentMinuteScopeKLinePo.java 838B
IFengFiveDayMinuteKLinePo.java 823B
TencentKLinePo.java 764B
TencentMinuteScopeNodeDataPo.java 725B
HKKLineNodeDataPo.java 723B
IFengFiveDayMinuteScopeKLinePo.java 709B
SZRealtimeMinuteKLinePo.java 699B
SinaBigDealPo.java 691B
TencentKLineNodeDataPo.java 687B
SinaRealtimeDealDetailPo.java 686B
NetsFQKLineNodeDataPo.java 686B
StockBaseTests.java 684B
TencentFiveDayMinuteKLinePo.java 671B
IFengRealtimeBigDealPo.java 670B
SinaMinuteKLineDataPo.java 669B
StockVo.java 668B
TencentRealtimeDealDetailPo.java 666B
SZKLinePo.java 659B
TencentRealtimePriceDealNumPo.java 651B
StockCategoryVo.java 624B
IFengKLinePo.java 622B
共 130 条
- 1
- 2
资源评论
JJJ69
- 粉丝: 6351
- 资源: 5918
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功