背景需求:
1、关注某开发商某项目在售楼盘每日网签数量 查询某栋楼的全部套数、已售套数、未售套数、面积、是否存在抵押。
2、依托官方提供的合法数据 获取后进行整合处理 用于个人查询使用
3、获取初始数据X楼盘X号楼当日总套数、已售套数、抵押情况等数据。存储数据后二次查询 如已售套数增加 则仅记录新售X楼盘X号楼X门牌号 并记录销售时间 新增销售套数 和新的已售总数
如已售总套数没有变化 则不记录新数据 。仅保留初始查询的结果作为初始数据,用于对比判断新售数据的X楼盘X号楼X门牌号 。
4、查询支持选择仅查询X楼盘或X号楼,查询结果储存在本地无互联网的测试环境 连接支持SSL加密或HTTP
部分实现 仅用于学习:
public class ParseJsonFromFile {
public int putjson(String jsonContent){
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedNow = now.format(formatter);
System.out.println("当前日期和时间: " + formattedNow);
String jsonString = jsonContent.toString();
JSONObject jsonObject = JSON.parseObject(jsonString);
// 获取 F_Rows 数组
JSONArray fRowsArray = jsonObject.getJSONArray("F_Rows");
List<JSONObject> soldList = new ArrayList<>(); // 用于存储已售记录
// 遍历每一行JSON数据
for (int i = 0; i < fRowsArray.size(); i++) {
JSONObject rowObject = fRowsArray.getJSONObject(i);
JSONArray f0Array = rowObject.getJSONArray("f0");
String saleStatus = f0Array.getString(3); // 销售状态
// 如果销售状态为“已售”,则将记录添加到列表中
if ("已售".equals(saleStatus)) {
soldList.add(rowObject);
}
}
// 总房产数量
int totalCount = fRowsArray.size();
// 已售房产数量
int soldCount = soldList.size();
// 根据楼号后四位进行排序
soldList.sort(new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
String buildingNumber1 = o1.getJSONArray("f0").getString(0);
String buildingNumber2 = o2.getJSONArray("f0").getString(0);
// 提取后四位并比较
String suffix1 = buildingNumber1.length() >= 4 ? buildingNumber1.substring(buildingNumber1.length() - 4) : buildingNumber1;
String suffix2 = buildingNumber2.length() >= 4 ? buildingNumber2.substring(buildingNumber2.length() - 4) : buildingNumber2;
return suffix1.compareTo(suffix2);
}
});
// 数据库连接配置
String url = "jdbc:mysql://local"; //
String username = ""; //
String password = ""; //
try (Connection connection = DriverManager.getConnection(url, username, password)) {
for (JSONObject rowObject : soldList) {
JSONArray f0Array = rowObject.getJSONArray("f0");
String buildingNumber = f0Array.getString(0); // 楼号
// 只获取前四个字符
String buildingNumberPrefix = buildingNumber.length() > 4 ? buildingNumber.substring(0, 4) : buildingNumber;
// 获取当前日期的年月日
LocalDateTime now1 = LocalDateTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); // 格式化为年月日
String currentDate = now1.format(dateFormatter);
// String currentDate = "20240802";
// 将前四个字符和日期组合
String bmValue = buildingNumberPrefix + currentDate;
// 查询当前数据库中的 cont 值
int currentCont = 0;
String selectCurrentContQuery ="SELECT cont FROM fang WHERE LEFT(bm, 4) = ? ORDER BY cont DESC LIMIT 1";
try (PreparedStatement selectCurrentContStmt = connection.prepareStatement(selectCurrentContQuery)) {
selectCurrentContStmt.setString(1, buildingNumberPrefix);
ResultSet resultSet = selectCurrentContStmt.executeQuery();
if (resultSet.next()) {
currentCont = resultSet.getInt("cont");
}
}
// 判断是否需要插入新记录
if (currentCont < soldCount) {
// 计算 newadd 值
int newaddValue = soldCount - currentCont;
// 插入新记录
String insert = "INSERT INTO fang (bm, cont, newadd) VALUES (?, ?, ?)";
try (PreparedStatement insertStatement = connection.prepareStatement(insert)) {
insertStatement.setString(1, bmValue);
insertStatement.setInt(2, soldCount);
insertStatement.setInt(3, newaddValue); // 设置 newadd 为 soldCount - currentCont
insertStatement.executeUpdate();
}
// 查询所有 bmValue 相同的记录
String selectQuery = "SELECT bm FROM fang WHERE bm = ?";
try (PreparedStatement selectStatement = connection.prepareStatement(selectQuery)) {
selectStatement.setString(1, bmValue);
ResultSet resultSet = selectStatement.executeQuery();
List<String> bmValues = new ArrayList<>();
while (resultSet.next()) {
bmValues.add(resultSet.getString("bm"));
}
// 删除多余记录,只保留第一条
if (bmValues.size() > 1) {
String deleteQuery = "DELETE FROM fang WHERE bm = ?";
try (PreparedStatement deleteStatement = connection.prepareStatement(deleteQuery)) {
for (int i = 1; i < bmValues.size(); i++) { // 从第二条开始删除
deleteStatement.setString(1, bmValues.get(i));
deleteStatement.executeUpdate();
}
}
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
// 打印排序后的已售记录
for (JSONObject rowObject : soldList) {
JSONArray f0Array = rowObject.getJSONArray("f0");
String buildingNumber = f0Array.getString(0); // 楼号
String area = f0Array.getString(1); // 面积
// System.out.println("楼号: " + buildingNumber + ", 面积: " + area);
}
// 输出总房产数量和已售房产数量
System.out.println("总房产数量: " + totalCount);
System.out.println("已售房产数量: " + soldCount);
return totalCount;
}
}
public class JsoupPostSSL {
public static void main(String[] args) {
ParseJsonFromFile a = new ParseJsonFromFile();
try {
// SSL
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
Connection.Response response = null;
// 发起POST请求
略
// 解析响应
Document doc = response.parse();
System.out.println(doc);
String json = null;
Elements scripts = doc.getElementsByTag("script");
for (Element script : scripts) {
String scriptData = script.data();
int startIndex = scriptData.indexOf("var f12_state");
// 查找 var f12_columns
int endIndex = scriptData.indexOf("var f12_columns");
if (startIndex != -1 && endIndex != -1) {
// 提取 JSON 数据的开始和结束位置
int jsonStart = scriptData.indexOf("{", startIndex);
// 找到 JSON 数据结束的地方,
int jsonEnd = endIndex > -1 ? scriptData.lastIndexOf("}", endIndex) : scriptData.length() - 1;
if (jsonStart != -1 && jsonEnd != -1) {
json = scriptData.substring(jsonStart, jsonEnd + 1);
// System.out.println("Extracted JSON: " + json);
a.putjson(json);
} else {
}
} else {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
暂无评论
要发表评论,您必须先 登录