從京東收貨地址介面抓取省、市、區、鎮四級行政區劃,輸出 JSON 供前端級聯選單使用。
class Address {
private String id;
private String name;
private List children;
// getter/setter 省略
}
package com.test.html;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class JD_Address {
private static final int TIMEOUT = 300000;
private static int count = 0;
public static void main(String[] args) throws IOException, InterruptedException {
String provinceUrl = "https://trade.jd.com/dynamic/consignee/getProvinces.action";
String cityUrl = "https://trade.jd.com/dynamic/consignee/getCitys.action?consigneeParam.provinceId=";
String countyUrl = "https://trade.jd.com/dynamic/consignee/getCountys.action?consigneeParam.cityId=";
String townUrl = "https://trade.jd.com/dynamic/consignee/getTowns.action?consigneeParam.countyId=";
List provinces = getList(provinceUrl);
for (Address province : provinces) {
List cities = getList(cityUrl + province.getId());
province.setChildren(cities);
for (Address city : cities) {
List counties = getList(countyUrl + city.getId());
city.setChildren(counties);
for (Address county : counties) {
List towns = getList(townUrl + county.getId());
county.setChildren(towns);
}
}
}
FileUtils.writeStringToFile(new File("address.js"), "var ds=" + JSON.toJSONString(provinces), "UTF-8");
System.out.println("完成,共 " + count + " 次請求");
}
private static List getList(String url) throws IOException, InterruptedException {
List list = new ArrayList<>();
Document doc = Jsoup.parse(new URL(url), TIMEOUT);
Elements options = doc.select("option[value~=\\d+]");
for (Element el : options) {
Address addr = new Address();
addr.setId(el.attr("value"));
addr.setName(el.text().replace("*", ""));
list.add(addr);
}
count++;
if (count % 100 == 0) System.out.println("已請求: " + count);
if (count % 500 == 0) Thread.sleep(5000); // 禮貌性延遲
return list;
}
}
var ds = [{
"id": "1", "name": "北京",
"children": [{
"id": "72", "name": "朝陽區",
"children": [{
"id": "2799", "name": "三環之內", "children": []
}, ...]
}, ...]
}, {
"id": "2", "name": "上海", "children": [...] }
];
org.jsoup
jsoup
1.16.1
com.alibaba
fastjson
1.2.83
commons-io
commons-io
2.11.0
org.apache.commons
commons-lang3
3.12.0