一、Jsoup
jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据。
官方链接:https://jsoup.org/
二、依赖引入
//jsoup依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
三、编写股票实体类
@Entity
@Table(name = "lh_stock")
public class Stock implements Serializable {
//主键为股票代码
@Id
private String stockCold;
//龙虎榜上榜天数
@Column
private String lHDay;
//股票名称
@Column
private String name;
@Column
//现价
private String xj;
//涨跌幅
@Column
private String zDF;
//成交金额
@Column
private String cJJE;
//净买入额
@Column
private String JMRE;
public Stock(){
}
public String getStockCold() {
return stockCold;
}
public void setStockCold(String stockCold) {
this.stockCold = stockCold;
}
public String getlHDay() {
return lHDay;
}
public void setlHDay(String lHDay) {
this.lHDay = lHDay;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getXj() {
return xj;
}
public void setXj(String xj) {
this.xj = xj;
}
public String getzDF() {
return zDF;
}
public void setzDF(String zDF) {
this.zDF = zDF;
}
public String getcJJE() {
return cJJE;
}
public void setcJJE(String cJJE) {
this.cJJE = cJJE;
}
public String getJMRE() {
return JMRE;
}
public void setJMRE(String JMRE) {
this.JMRE = JMRE;
}
}
四、编写控制层
@Controller
public class StockController {
@Autowired
private StockRepository stockRepository;
//爬取同花顺龙虎榜信息,并保存到数据库
@RequestMapping("/LHB")
public String lhStock() throws IOException {
try{
//获取页面Document对象
Document document = Jsoup.connect("http://data.10jqka.com.cn/market/longhu/#refCountId=db_50754c78_378").get();
//查找并获取table下的tr节点
Elements elements = document.select("div.twrap").select("table.m-table").select("tr");
//遍历tr节点
for (Element element : elements){
//获取tr子节点对象
Elements childElements = element.select("tr").first().children();
//获取数据
String lHDay = childElements.get(0).text();
if (lHDay==null){
lHDay = "首日";
}
String stockCold = childElements.get(1).text();
String name = childElements.get(2).text();
String xj = childElements.get(3).text();
String zDF = childElements.get(4).text();
String cJJE = childElements.get(5).text();
String jMRE = childElements.get(6).text();
//创建Stock对象
Stock stock = new Stock();
stock.setStockCold(stockCold);
stock.setlHDay(lHDay);
stock.setName(name);
stock.setXj(xj);
stock.setzDF(zDF);
stock.setJMRE(jMRE);
stock.setcJJE(cJJE);
//保存到数据库
stockRepository.save(stock);
}
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/toStockList";
}
//跳转龙虎榜页面
@RequestMapping("/toStockList")
public String toStockList(Model model){
List<Stock> stocks = stockRepository.findAll();
model.addAttribute("stocks",stocks);
return "LHB-list";
}
}
五、编写页面
<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>龙虎榜</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" th:href="@{/css/font.css}">
<link rel="stylesheet" th:href="@{/css/xadmin.css}">
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="x-nav">
<span class="layui-breadcrumb">
<a href="">首页</a>
<a>
<cite>导航元素</cite></a>
</span>
<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
<div class="layui-row layui-col-space15">
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-body ">
<div class="layui-inline layui-show-xs-block">
<input type="text" name="username" placeholder="请输入编号 " autocomplete="off" class="layui-input">
</div>
<div class="layui-inline layui-show-xs-block">
<button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon"></i></button>
</div>
</form>
</div>
<div class="layui-card-body layui-table-body layui-table-main">
<table class="layui-table layui-form">
<thead>
<tr>
<th>
<input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
</th>
<th>上榜</th>
<th>股票代码</th>
<th>股票名称</th>
<th>现价</th>
<th>涨跌幅</th>
<th>成交金额</th>
<th>净买入额</th>
</tr>
</thead>
<tbody>
<tr th:each="stock:${stocks}">
<td>
<input type="checkbox" name="id" value="1" lay-skin="primary">
</td>
<td>[[${stock.lHDay}]]</td>
<td>[[${stock.stockCold}]]</td>
<td>[[${stock.name}]]</td>
<td>[[${stock.xj}]]</td>
<td>[[${stock.zDF}]]</td>
<td>[[${stock.cJJE}]]</td>
<td>[[${stock.jMRE}]]</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>