diff --git a/.idea/ApifoxUploaderProjectSetting.xml b/.idea/ApifoxUploaderProjectSetting.xml new file mode 100644 index 00000000..c90886ef --- /dev/null +++ b/.idea/ApifoxUploaderProjectSetting.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/main/java/com/dsideal/base/Test/TestTongYi.java b/src/main/java/com/dsideal/base/Test/TestTongYi.java new file mode 100644 index 00000000..7331b428 --- /dev/null +++ b/src/main/java/com/dsideal/base/Test/TestTongYi.java @@ -0,0 +1,82 @@ +package com.dsideal.base.Test; + +import com.alibaba.dashscope.exception.InputRequiredException; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.dsideal.base.DataEase.Model.DataEaseModel; +import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.sql.SQLOutput; +import java.util.List; + +import com.jfinal.plugin.activerecord.Record; + +public class TestTongYi { + + public static String getHTML(String url) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + URL website = new URL(url); + HttpURLConnection connection = (HttpURLConnection) website.openConnection(); + connection.setRequestMethod("GET"); + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + } + reader.close(); + return stringBuilder.toString(); + } + + /** + * 按县区名称获取面积和镇乡信息 + * + * @param areaName + * @return + * @throws IOException + */ + public static String getXq(String areaName) throws IOException, InterruptedException { + String res = ""; + String url = "https://baike.baidu.com/item/" + areaName + "?fromModule=lemma_search-box"; + String htmlContent = getHTML(url); + + + // 从字符串解析HTML + Document doc = Jsoup.parse(htmlContent); + + // 选择所有span标签 + Elements spans = doc.select("span"); + // 遍历所有span标签 + for (Element span : spans) { + // 检查span的文本是否包含"km²" + if (span.text().contains("km²")) { + // 输出符合条件的span内容 + res = span.text().replace("km²", "").trim(); + } + if (span.text().contains("个镇") && span.text().contains("个乡") && span.text().contains("、") && span.text().length() <= 12) { + res = res + "," + span.text(); + } + } + return res; + } + + public static void main(String[] args) throws NoApiKeyException, InputRequiredException, IOException, InterruptedException { + //初始化数据库连接 + LocalMysqlConnectUtil.Init(); + DataEaseModel dm = new DataEaseModel(); + List list = dm.getProvinceArea("云南省"); + for (Record record : list) { + String areaName=record.getStr("area_name"); + String res = getXq(areaName); + System.out.println(areaName + "\t" + res); + Thread.sleep(3000); + } + } +}