crawler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const http = require('http')
  2. const iconv = require('iconv-lite')
  3. const minify = require('html-minifier').minify
  4. const BufferHelper = require('bufferhelper')
  5. /*
  6. * 命名简写备注
  7. *
  8. * 省级(省份,Province) p
  9. * 地级(城市,City) c
  10. * 县级(区县,Area) a
  11. * 乡级(乡镇街道,Street) s
  12. * 村级(村委会居委会,Village) v
  13. */
  14. const pReg = /<td><a href='(.*?).html'>(.*?)<br><\/a><\/td>/g
  15. const casReg = /<tr class='.*?'><td><a href=.*?>(.*?)<\/a><\/td><td><a href=.*?>(.*?)<\/a><\/td><\/tr>/g
  16. const vReg = /<tr class='.*?'><td>(.*?)<\/td><td>.*?<\/td><td>(.*?)<\/td><\/tr>/g
  17. const host = 'www.stats.gov.cn'
  18. const path = '/tjsj/tjbz/tjyqhdmhcxhfdm/2018/#{route}.html'
  19. /**
  20. * 抓取数据
  21. * @author modood <https://github.com/modood>
  22. * @datetime 2018-01-31 19:23
  23. */
  24. exports.fetch = (host, route, regexp, codeLen) =>
  25. new Promise((resolve, reject) => http.get({
  26. host,
  27. path: path.replace('#{route}', route),
  28. timeout: 3000
  29. }, res => {
  30. const bufferHelper = new BufferHelper()
  31. const statusCode = res.statusCode
  32. if (statusCode !== 200) {
  33. res.resume()
  34. return reject(new Error('Request Failed. Status Code: ' + statusCode))
  35. }
  36. res.on('data', chunk => bufferHelper.concat(chunk))
  37. res.on('end', () => {
  38. const rawData = minify(iconv.decode(bufferHelper.toBuffer(), 'GBK'), { collapseWhitespace: true, quoteCharacter: '\'' })
  39. const result = {}
  40. let current
  41. while ((current = regexp.exec(rawData)) !== null) result[current[1].substr(0, codeLen)] = current[2].trim()
  42. return resolve(result)
  43. })
  44. }).on('error', reject).on('timeout', () => reject(new Error('timeout'))))
  45. /**
  46. * 抓取省级数据
  47. * @author modood <https://github.com/modood>
  48. * @datetime 2018-01-31 19:40
  49. */
  50. exports.fetchProvinces = async () => {
  51. try {
  52. return await exports.fetch(host, 'index', pReg, 2)
  53. } catch (err) {
  54. if (err.message !== 'timeout') console.log(`抓取省级数据失败(${err}),正在重试...`)
  55. return exports.fetchProvinces()
  56. }
  57. }
  58. /**
  59. * 抓取地级数据
  60. * @author modood <https://github.com/modood>
  61. * @datetime 2018-01-31 19:51
  62. */
  63. exports.fetchCities = async (pCode) => {
  64. try {
  65. return await exports.fetch(host, pCode, casReg, 4)
  66. } catch (err) {
  67. if (err.message !== 'timeout') console.log(`抓取省级(${pCode})的地级数据失败(${err}),正在重试...`)
  68. return exports.fetchCities(pCode)
  69. }
  70. }
  71. /**
  72. * 抓取县级数据
  73. * @author modood <https://github.com/modood>
  74. * @datetime 2018-01-31 20:03
  75. */
  76. exports.fetchAreas = async (cCode) => {
  77. cCode = cCode.toString()
  78. const pCode = cCode.substr(0, 2)
  79. try {
  80. return await exports.fetch(host, `${pCode}/${cCode}`, casReg, 6)
  81. } catch (err) {
  82. if (err.message !== 'timeout') console.log(`抓取地级(${cCode})的县级数据失败(${err}),正在重试...`)
  83. return exports.fetchAreas(cCode)
  84. }
  85. }
  86. /**
  87. * 抓取乡级数据
  88. * @author modood <https://github.com/modood>
  89. * @datetime 2018-01-31 20:08
  90. */
  91. exports.fetchStreets = async (aCode, route) => {
  92. aCode = aCode.toString()
  93. const pCode = aCode.substr(0, 2)
  94. const cCodeSuffix = aCode.substr(2, 2)
  95. const _route = route || `${pCode}/${cCodeSuffix}/${aCode}`
  96. try {
  97. return await exports.fetch(host, _route, casReg, 9)
  98. } catch (err) {
  99. if (err.message !== 'timeout') console.log(`抓取县级(${aCode})的乡级数据失败(${err}),正在重试...`)
  100. return exports.fetchStreets(aCode, route)
  101. }
  102. }
  103. /**
  104. * 抓取村级数据
  105. * @author modood <https://github.com/modood>
  106. * @datetime 2018-01-31 20:19
  107. */
  108. exports.fetchVillages = async (sCode, route) => {
  109. sCode = sCode.toString()
  110. const pCode = sCode.substr(0, 2)
  111. const cCodeSuffix = sCode.substr(2, 2)
  112. const aCodeSuffix = sCode.substr(4, 2)
  113. const _route = route || `${pCode}/${cCodeSuffix}/${aCodeSuffix}/${sCode}`
  114. try {
  115. return await exports.fetch(host, _route, vReg, 12)
  116. } catch (err) {
  117. if (err.message !== 'timeout') console.log(`抓取乡级(${sCode})的村级数据失败(${err}),正在重试...`)
  118. return exports.fetchVillages(sCode, route)
  119. }
  120. }