parent
fd9e255cff
commit
16594d75e1
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ApifoxUploaderProjectSetting">
|
||||||
|
<option name="apiAccessToken" value="APS-Kc3YrVfGlTrgg0vw2qCGNG29jJ62vPRz" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -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<Record> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue