找了很多都只有英文,并且 Hong Kong, Macao, Taiwan都是单独列出来的。
发现QQ注册页的国家和城市数据比较全面。可以把它分离出来。
数据来源 http://zc.qq.com/chs/index.html
js:http://4.url.cn/zc/chs/js/10062/location_chs.js
不确定这个数据结构什么时候会变,所以我分析一个数据,生成sql语句,保存到数据库里(MySQL)。
建数据库:
CREATE TABLE `t_location` ( `location_id` int(11) NOT NULL AUTO_INCREMENT, `abbr` varchar(30) NOT NULL DEFAULT '', `name_chs` varchar(30) NOT NULL DEFAULT '', `name_cht` varchar(30) NOT NULL DEFAULT '', `name_en` varchar(30) NOT NULL DEFAULT '', `location_type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0:country,1:state,2:city', `parent_id` int(11) NOT NULL DEFAULT '0' COMMENT 'parent location_id', `is_visible` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0:visible,1:invisible', PRIMARY KEY (`location_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
获取js对象
var local; function initLocation(data) { local = data; } $.getScript('http://4.url.cn/zc/chs/js/10062/location_chs.js');
获取 sql
var countryId = 0; var locationId = 0; var countrySql = ''; var provinceSql = ''; var citySql = ''; $.each(local, function(k, v) { if (!v.n.length) { return; } countryId++; locationId++; countrySql += 'insert into t_location(location_id, abbr, name_chs) values(' + countryId + ',\'' + k + '\',\'' + v.n + '\');'; $.each(v, function(k2, v2) { if (typeof(v2.n) === 'undefined' || !v2.n.length) { return; } provinceSql += 'insert into t_location(parent_id,location_type, abbr, name_chs) values(\'' + countryId + '\',1,\'' + k2 + '\',\'' + v2.n + '\');';; locationId++; }); }); $(document.body).html(''); $(document.body).append(countrySql + provinceSql); var pid = countryId; countryId = 0; $.each(local, function(k, v) { if (!v.n.length) { return; } locationId++; countryId++; $.each(v, function(k2, v2) { if (typeof(v2.n) === 'undefined') { return; } if (!v2.n.length) { $.each(v2, function(k, v) { if (k == 0 || k === 'p' || typeof(v.n) === 'undefined') { return; } //没有省级,国家下面直接是城市 citySql += 'insert into t_location(parent_id,location_type, abbr,name_chs) values(\'' + countryId + '\',2,\'' + k + '\',\'' + v.n + '\');';; }); return; } pid++; $.each(v2, function(k, v) { if (k === 'n' || !v.n.length) { return; } citySql += 'insert into t_location(parent_id,location_type, abbr, name_chs) values(\'' + pid + '\',2,\'' + k + '\',\'' + v.n + '\');'; }); }); }); $(document.body).append(citySql);