代碼如下:
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 int timeout = 300000;
private static int count = 0;
public static void main(String[] args) throws IOException, InterruptedException
{
String province_url = "http://trade.jd.com/dynamic/consignee/getProvinces.action";
String city_url = "http://trade.jd.com/dynamic/consignee/getCitys.action?consigneeParam.provinceId=";
String county_url = "http://trade.jd.com/dynamic/consignee/getCountys.action?consigneeParam.cityId=";
String town_url = "http://trade.jd.com/dynamic/consignee/getTowns.action?consigneeParam.countyId=";
//getList(town_url + "4139");
List<Address> provinces = getList(province_url);
for (Address province : provinces)
{
List<Address> citys = getList(city_url + province.getId());
province.setChildren(citys);
for (Address city : citys)
{
List<Address> countys = getList(county_url + city.getId());
city.setChildren(countys);
for (Address county : countys)
{
List<Address> towns = getList(town_url + county.getId());
county.setChildren(towns);
}
}
}
System.out.println("=======");
FileUtils.writeStringToFile(new File("保存位置/address2.js"), "var ds=" + JSON.toJSONString(provinces));
}
private static List<Address> getList(String url) throws IOException, InterruptedException
{
List<Address> list = new ArrayList<Address>();
Document doc = Jsoup.parse(new URL(url), timeout);
Elements elements = doc.select("option[value~=\\d+]");
for (Element element : elements)
{
Address address = new Address();
address.setName(element.text().replace("*", StringUtils.EMPTY));
address.setId(element.attr("value"));
list.add(address);
//System.out.println(address.getName());
}
++count;
if (count % 100 == 0)
{
System.out.println(count);
}
if (count % 500 == 0)
{
Thread.sleep(5000);
}
return list;
}
}
class Address
{
private String name;
private String id;
private List<Address> children;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<Address> getChildren()
{
return children;
}
public void setChildren(List<Address> children)
{
this.children = children;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
JavaScript]代碼
代碼如下:
[
{"children": [
{"children": [
{"id": "2799", "name": "三環以內"},
{"id": "2819", "name": "三環到四環之間"},
{"id": "2839", "name": "四環到五環之間"},
{"id": "2840", "name": "五環到六環之間"},
{"id": "4137", "name": "管莊"},
{"id": "4139", "name": "北苑"},
{"id": "4211", "name": "定福莊"}
], "id": "72", "name": "朝陽區"},
{"children": [
{"id": "2848", "name": "三環以內"},
{"id": "2849", "name": "三環到四環之間"},
{"id": "2850", "name": "四環到五環之間"},
{"id": "2851", "name": "五環到六環之間"},
{"id": "2852", "name": "六環以外"},
{"id": "4134", "name": "西三旗"},
{"id": "4209", "name": "西二旗"}
], "id": "2800", "name": "海澱區"},
{"children": [
{"id": "2827", "name": "內環到二環裡"},
{"id": "2853", "name": "二環到三環"}
], "id": "2801", "name": "西城區"},
{"children": [
{"id": "2821", "name": "內環到三環裡"}
], "id": "2802", "name": "東城區"},
{"children": [
{"id": "2829", "name": "一環到二環"},
{"id": "2842", "name": "二環到三環"}
], "id": "2803", "name": "崇文區"},
{"children": [
{"id": "2828", "name": "內環到三環裡"}
], "id": "2804", "name": "宣武區"},
{"children": [
{"id": "2832", "name": "四環到五環之間"},
{"id": "2854", "name": "二環到三環"},
{"id": "2855", "name": "三環到四環之間"},
{"id": "34544", "name": "五環到六環之間"},
{"id": "34545", "name": "六環之外"}
], "id": "2805", "name": "豐台區"},
{"children": [
{"id": "2831", "name": "四環到五環內"},
{"id": "4187", "name": "石景山城區"},
{"id": "4188", "name": "八大處科技園區"}
], "id": "2806", "name": "石景山區"},
{"children": [
{"id": "6491", "name": "城區以內"},
{"id": "2843", "name": "郊區"}
], "id": "2807", "name": "門頭溝"},
{"children": [
{"id": "6492", "name": "城區以內"},
{"id": "2844", "name": "郊區"}
], "id": "2808", "name": "房山區"},
{"children": [
{"id": "4175", "name": "五環到六環之間"},
{"id": "37643", "name": "六環以外(於家務鄉)"},
{"id": "51150", "name": "六環以外(張家灣鎮、台湖鎮、漷縣鎮)"},
{"id": "51151", "name": "六環以外(宋莊鎮)"},
{"id": "35698", "name": "六環以外(其他地區)"}
], "id": "2809", "name": "通州區"},
{"children": [
{"id": "4194", "name": "四環至五環之間"},
{"id": "6501", "name": "五環至六環之間"},
{"id": "4205", "name": "六環以外"},
{"id": "51081", "name": "亦莊經濟開發區"}
], "id": "2810", "name": "大興區"},
{"children": [
{"id": "51125", "name": "北石槽鎮"},
{"id": "51126", "name": "北務鎮"},
{"id": "51127", "name": "北小營鎮"},
{"id": "51128", "name": "大孫各莊鎮"},
{"id": "51129", "name": "高麗營鎮"},
{"id": "51130", "name": "光明街道"},
{"id": "51131", "name": "後沙峪地區"},
{"id": "51132", "name": "空港街道"},
{"id": "51133", "name": "李橋鎮"},
{"id": "51134", "name": "李遂鎮"},
{"id": "51135", "name": "龍灣屯鎮"},
{"id": "51136", "name": "馬坡地區"},
{"id": "51137", "name": "木林鎮"},
{"id": "51138", "name": "南彩鎮"},
{"id": "51139", "name": "南法信地區"},
{"id": "51140", "name": "牛欄山地區"},
{"id": "51141", "name": "仁和地區"},
{"id": "51142", "name": "勝利街道"},
{"id": "51143", "name": "石園街道"},
{"id": "51144", "name": "雙豐街道"},
{"id": "51145", "name": "天竺地區"},
{"id": "51146", "name": "旺泉街道"},
{"id": "51147", "name": "楊鎮地區"},
{"id": "51148", "name": "張鎮"},
{"id": "51149", "name": "趙全營鎮"}
], "id": "2812", "name": "順義區"},
{"children": [
{"id": "6115", "name": "城區以內"},
{"id": "2847", "name": "郊區"}
], "id": "2814", "name": "懷柔區"},
{"children": [
{"id": "6667", "name": "城區"},
{"id": "2862", "name": "城區以外"}
], "id": "2816", "name": "密雲區"},
{"children": [
{"id": "4135", "name": "六環以內"},
{"id": "4136", "name": "城區"},
{"id": "2906", "name": "城區以外"}
], "id": "2901", "name": "昌平區"},
{"children": [
{"id": "6666", "name": "城區"},
{"id": "2954", "name": "城區以外"}
], "id": "2953", "name": "平谷區"},
{"children": [
{"id": "6009", "name": "百泉路北,京新高速南,康張路東,京銀路西"},
{"id": "3066", "name": "百泉路南,京新高速北,康張路西,京銀路東"}
], "id": "3065", "name": "延慶縣"}
], "id": "1", "name": "北京"},
{"children": [
{"children": [
{"id": "79", "name": "內環以內"},
{"id": "80", "name": "內環中環之間"},
{"id": "81", "name": "中環外環之間"}
], "id": "78", "name": "黃浦區"},
{"children": [], "id": "2811", "name": "盧灣區"},
{"children": [
{"id": "2863", "name": "內環以內"},
{"id": "2865", "name": "內環中環之間"}
], "id": "2813", "name": "徐匯區"},
{"children": [
{"id": "2870", "name": "內環以內"},
{"id": "2871", "name": "內環中環之間"},
{"id": "2872", "name": "中環外環之間"}
], "id": "2815", "name": "長寧區"},
{"children": [
{"id": "2873", "name": "內環以內"},
{"id": "2874", "name": "內環中環之間"},
{"id": "2875", "name": "中環外環之間"}
], "id": "2817", "name": "靜安區"},
{"children": [], "id": "2820", "name": "閘北區"},
{"children": [
{"id": "2856", "name": "內環中環之間"},
{"id": "2882", "name": "內環以內"},
{"id": "2883", "name": "中環外環之間"}
], "id": "2822", "name": "虹口區"},
{"children": [], "id": "2823", "name": "楊浦區"},
{"children": [
{"id": "2889", "name": "中環以內"},
{"id": "2890", "name": "中環外環之間"},
{"id": "2891", "name": "外環以外"}
], "id": "2824", "name": "寶山區"},
{"children": [
{"id": "2892", "name": "中環以內"},
{"id": "2893", "name": "外環以外"},
{"id": "2896", "name": "中環外環之間"}
], "id": "2825", "name": "闵行區"},
{"children": [
{"id": "2864", "name": "外環以外"}
], "id": "2826", "name": "嘉定區"},
{"children": [
{"id": "2894", "name": "中環以內"},
{"id": "2895", "name": "外環以外"},
{"id": "2897", "name": "中環外環之間"}
], "id": "2830", "name": "浦東新區"},
{"children": [
{"id": "2869", "name": "外環以外"}
], "id": "2833", "name": "青浦區"},
{"children": [
{"id": "2866", "name": "外環以外"}
], "id": "2834", "name": "松江區"},
{"children": [
{"id": "2868", "name": "外環以外"}
], "id": "2835", "name": "金山區"},
{"children": [
{"id": "2903", "name": "祝橋鎮"},
{"id": "2904", "name": "新場鎮"},
{"id": "2935", "name": "惠南鎮"},
{"id": "2937", "name": "康橋鎮"},
{"id": "2938", "name": "宣橋鎮"},
{"id": "2939", "name": "書院鎮"},
{"id": "2940", "name": "大團鎮"},
{"id": "2941", "name": "周浦鎮"},
{"id": "2942", "name": "蘆潮港鎮"},
{"id": "2943", "name": "泥城鎮"},
{"id": "2944", "name": "六灶鎮"},
{"id": "2945", "name": "航頭鎮"},
{"id": "2946", "name": "萬祥鎮"},
{"id": "2947", "name": "老港鎮"},
{"id": "4159", "name": "申港街道"},
{"id": "4180", "name": "臨港新城"}
], "id": "2836", "name": "南匯區"},
{"children": [
{"id": "2888", "name": "外環以外"}
], "id": "2837", "name": "奉賢區"},
{"children": [
{"id": "2876", "name": "內環以內"},
{"id": "2877", "name": "內環中環之間"},
{"id": "2878", "name": "中環外環之間"},
{"id": "3110", "name": "桃浦新村"}
], "id": "2841", "name": "普陀區"},
{"children": [
{"id": "50779", "name": "堡鎮"},
{"id": "50780", "name": "廟鎮"},
{"id": "50781", "name": "陳家鎮"},
{"id": "50782", "name": "城橋鎮"},
{"id": "50783", "name": "東平鎮"},
{"id": "50784", "name": "港西鎮"},
{"id": "50785", "name": "港沿鎮"},
{"id": "50786", "name": "建設鎮"},
{"id": "50787", "name": "綠華鎮"},
{"id": "50788", "name": "三星鎮"},
{"id": "50789", "name": "豎新鎮"},
{"id": "50790", "name": "向化鎮"},
{"id": "50791", "name": "新海鎮"},
{"id": "50792", "name": "新河鎮"},
{"id": "50793", "name": "中興鎮"},
{"id": "50794", "name": "長興鄉"},
{"id": "50795", "name": "橫沙鄉"},
{"id": "50796", "name": "新村鄉"}
], "id": "2919", "name": "崇明縣"}
], "id": "2", "name": "上海"},
{"children": [
{"children": [
{"id": "39620", "name": "全境"}
], "id": "51035", "name": "東麗區"},
{"children": [
{"id": "2984", "name": "全境"}
], "id": "51036", "name": "和平區"},
{"children": [
{"id": "2987", "name": "全境"}
], "id": "51037", "name": "河北區"},
{"children": [
{"id": "3000", "name": "全境"}
], "id": "51038", "name": "河東區"},
{"children": [
{"id": "2985", "name": "全境"}
], "id": "51039", "name": "河西區"},
{"children": [
{"id": "2986", "name": "全境"}
], "id": "51040", "name": "紅橋區"},
{"children": [
{"id": "98", "name": "全境"}
], "id": "51041", "name": "薊縣"},
{"children": [
{"id": "36157", "name": "全境"}
], "id": "51042", "name": "靜海縣"},
{"children": [
{"id": "2907", "name": "全境"}
], "id": "51043", "name": "南開區"},
{"children": [
{"id": "25708", "name": "全境"}
], "id": "51044", "name": "塘沽區"},
{"children": [
{"id": "25712", "name": "楊柳青,中北,精武,大寺鎮,環外海泰及外環內"},
{"id": "25711", "name": "其它地區"}
], "id": "51045", "name": "西青區"},
{"children": [
{"id": "22846", "name": "楊村鎮、下朱莊內"},
{"id": "22847", "name": "其它地區"}
], "id": "51046", "name": "武清區"},
{"children": [
{"id": "25704", "name": "鹹水沽鎮、海河教育園,海河科技園"},
{"id": "36171", "name": "雙港,辛莊"},
{"id": "36172", "name": "其他地區"}
], "id": "51047", "name": "津南區"},
{"children": [
{"id": "23672", "name": "漢沽區街裡、漢沽開發區"},
{"id": "23673", "name": "其它地區"}
], "id": "51048", "name": "漢沽區"},
{"children": [], "id": "51049", "name": "大港區"},
{"children": [
{"id": "6646", "name": "外環內"},
{"id": "36167", "name": "外環外雙街鎮,河北工大新校,屈店工業園"},
{"id": "36168", "name": "外環外其它地區"}
], "id": "51050", "name": "北辰區"},
{"children": [
{"id": "22848", "name": "城關鎮、馬家店開發區、天寶工業園"},
{"id": "22849", "name": "其它地區"}
], "id": "51051", "name": "寶坻區"},
{"children": [
{"id": "23674", "name": "蘆台鎮、經濟開發區、貿易開發區"},
{"id": "23675", "name": "其它地區"}
], "id": "51052", "name": "寧河縣"}
], "id": "3", "name": "天津"},
{"children": [
{"children": [
{"id": "9775", "name": "陳家壩街道"},
{"id": "9776", "name": "鐘鼓樓街道"},
{"id": "9777", "name": "周家壩街道"},
{"id": "9778", "name": "百安壩街道"},
{"id": "9779", "name": "高筍塘街道"},
{"id": "9780", "name": "雙河口街道"},
{"id": "9781", "name": "龍都街道"},
{"id": "9782", "name": "牌樓街道"},
{"id": "9783", "name": "沙河街道"},
{"id": "9784", "name": "太白街道"},
{"id": "9785", "name": "五橋街道"},
{"id": "9786", "name": "白土鎮"},
{"id": "9787", "name": "白羊鎮"},
{"id": "9788", "name": "大周鎮"},
{"id": "9789", "name": "彈子鎮"},
{"id": "9790", "name": "分水鎮"},
{"id": "9791", "name": "甘寧鎮"},
{"id": "9792", "name": "高峰鎮"},
{"id": "9793", "name": "高梁鎮"},
{"id": "9794", "name": "後山鎮"},
{"id": "9795", "name": "李河鎮"},
{"id": "9796", "name": "龍駒鎮"},
{"id": "9797", "name": "龍沙鎮"},
{"id": "9798", "name": "羅田鎮"},
{"id": "9799", "name": "孫家鎮"},
{"id": "9800", "name": "太安鎮"},
{"id": "9801", "name": "太龍鎮"},
{"id": "9802", "name": "天城鎮"},
{"id": "9803", "name": "武陵鎮"},
{"id": "9804", "name": "響水鎮"},
{"id": "9805", "name": "小周鎮"},
{"id": "9806", "name": "新田鎮"},
{"id": "9807", "name": "新鄉鎮"},
{"id": "9808", "name": "熊家鎮"},
{"id": "9809", "name": "余家鎮"},
{"id": "9810", "name": "長嶺鎮"},
{"id": "9811", "name": "長坪鎮"},
{"id": "9812", "name": "長灘鎮"},
{"id": "9813", "name": "走馬鎮"},
{"id": "9814", "name": "瀼渡鎮"},
{"id": "9815", "name": "茨竹鄉"},
{"id": "9816", "name": "柱山鄉"},
{"id": "9817", "name": "燕山鄉"},
{"id": "9818", "name": "溪口鄉"},
{"id": "9819", "name": "普子鄉"},
{"id": "9820", "name": "地寶鄉"},
{"id": "9821", "name": "鐵峰鄉"},
{"id": "9822", "name": "黃柏鄉"},
{"id": "9823", "name": "九池鄉"},
{"id": "9824", "name": "梨樹鄉"},
{"id": "9825", "name": "郭村鄉"},
{"id": "9826", "name": "恆合鄉"}
], "id": "113", "name": "萬州區"},
{"children": [
{"id": "9893", "name": "荔枝街道"},
{"id": "9894", "name": "敦仁街道"},
{"id": "9895", "name": "江北街道"},
{"id": "9896", "name": "江東街道"},
{"id": "9897", "name": "崇義街道"},
{"id": "9898", "name": "李渡鎮"},
{"id": "9899", "name": "白濤鎮"},
{"id": "9900", "name": "百勝鎮"},
{"id": "9901", "name": "堡子鎮"},
{"id": "9902", "name": "焦石鎮"},
{"id": "9903", "name": "蔺市鎮"},
{"id": "9904", "name": "龍橋鎮"},
{"id": "9905", "name": "龍潭鎮"},
{"id": "9906", "name": "馬武鎮"},
{"id": "9907", "name": "南沱鎮"},
{"id": "9908", "name": "青羊鎮"},
{"id": "9909", "name": "清溪鎮"},
{"id": "9910", "name": "石沱鎮"},
{"id": "9911", "name": "新妙鎮"},
{"id": "9912", "name": "義和鎮"},
{"id": "9913", "name": "增福鄉"},
{"id": "9914", "name": "珍溪鎮"},
{"id": "9915", "name": "鎮安鎮"},
{"id": "9916", "name": "致韓鎮"},
{"id": "9917", "name": "土地坡鄉"},
{"id": "9918", "name": "武陵山鄉"},
{"id": "9919", "name": "中峰鄉"},
{"id": "9920", "name": "梓裡鄉"},
{"id": "9921", "name": "叢林鄉"},
{"id": "9922", "name": "大木鄉"},
{"id": "9923", "name": "惠民鄉"},
{"id": "9924", "name": "酒店鄉"},
{"id": "9925", "name": "聚寶鄉"},
{"id": "9926", "name": "卷洞鄉"},
{"id": "9927", "name": "兩匯鄉"},
{"id": "9928", "name": "羅雲鄉"},
{"id": "9929", "name": "明家鄉"},
{"id": "9930", "name": "仁義鄉"},
{"id": "9931", "name": "山窩鄉"},
{"id": "9932", "name": "石和鄉"},
{"id": "9933", "name": "石龍鄉"},
{"id": "9934", "name": "太和鄉"},
{"id": "9935", "name": "天台鄉"},
{"id": "9936", "name": "同樂鄉"},
{"id": "9937", "name": "新村鄉"}
], "id": "114", "name": "涪陵區"},
{"children": [
{"id": "9938", "name": "梁山鎮"},
{"id": "9939", "name": "柏家鎮"},
{"id": "9940", "name": "碧山鎮"},
{"id": "9941", "name": "大觀鎮"},
{"id": "9942", "name": "福祿鎮"},
{"id": "9943", "name": "合興鎮"},
{"id": "9944", "name": "和林鎮"},
{"id": "9945", "name": "虎城鎮"},
{"id": "9946", "name": "回龍鎮"},
{"id": "9947", "name": "金帶鎮"},
{"id": "9948", "name": "聚奎鎮"},
{"id": "9949", "name": "禮讓鎮"},
{"id": "9950", "name": "龍門鎮"},
{"id": "9951", "name": "明達鎮"},
{"id": "9952", "name": "蟠龍鎮"},
{"id": "9953", "name": "屏錦鎮"},
{"id": "9954", "name": "仁賢鎮"},
{"id": "9955", "name": "石安鎮"},
{"id": "9956", "name": "文化鎮"},
{"id": "9957", "name": "新盛鎮"},
{"id": "9958", "name": "蔭平鎮"},
{"id": "9959", "name": "袁驿鎮"},
{"id": "9960", "name": "雲龍鎮"},
{"id": "9961", "name": "竹山鎮"},
{"id": "9962", "name": "安勝鄉"},
{"id": "9963", "name": "鐵門鄉"},
{"id": "9964", "name": "紫照鄉"},
{"id": "9965", "name": "曲水鄉"},
{"id": "9966", "name": "龍勝鄉"},
{"id": "9967", "name": "城北鄉"},
{"id": "9968", "name": "城東鄉"},
{"id": "9969", "name": "復平鄉"},
{"id": "39680", "name": "縣城內"}
], "id": "115", "name": "梁平縣"},