Skip to content

Instantly share code, notes, and snippets.

@suhaotian
Last active June 22, 2017 07:17
Show Gist options
  • Save suhaotian/5d9d4e96049c8b74b2585ee4637b27c1 to your computer and use it in GitHub Desktop.
Save suhaotian/5d9d4e96049c8b74b2585ee4637b27c1 to your computer and use it in GitHub Desktop.
/*
introduction: 中国最新省市区数据
发布时间:2017-03-10 10:33
usage:
- open http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html
- copy below lines code to console (F12)
data: https://gist.githubusercontent.com/suhaotian/cd31a6ca16e5298a1aae3dc7871d0660/raw/368916778e2cd8c0fa15f378879fa89dadaa96e8/china-address.json
*/
let provinces = []
let obj = {}, city = [], area = []
$('.TRS_PreAppend p').toArray().forEach(i => {
let $i = $(i)
let text = $i.text().trim().split(' ')
text[0] = text[0].trim()
text[1] = text[1].trim()
if ($i.find('> b').length == 2) {
// console.log('省', $i.text().trim())
if (obj.name && 1) {
obj.city= city
provinces.push(obj)
}
obj = {}
city = []
area = []
obj = {
code: text[0],
name: text[1],
}
} else {
let $s = $i.find('span[lang=EN-US] span').text()
if ($s.length == 5) {
// console.log('区', $i.text().trim())
city[city.length-1].area.push({
code: text[0],
name: text[1],
})
} else if ($s.length == 9) {
city.push({
code: text[0],
name: text[1],
area: []
})
}
}
})
// download
var blob = new Blob([JSON.stringify(provinces, null, 4)], {type: "octet/stream"})
var a = document.createElement('a')
a.download = 'china.address.json' // filename
a.href = window.URL.createObjectURL(blob)
document.body.appendChild(a)
a.click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment