获取行政区划代码

爬取国家统计局最新县及县以上行政区划代码, 解析为json。http://www.mca.gov.cn/article/sj/tjbz/a/

  1. // ==UserScript==
  2. // @name 获取行政区划代码
  3. // @namespace https://github.com/Ahaochan/Tampermonkey
  4. // @version 0.0.3
  5. // @description 爬取国家统计局最新县及县以上行政区划代码, 解析为json。http://www.mca.gov.cn/article/sj/tjbz/a/
  6. // @author Ahaochan
  7. // @include /http://www\.mca\.gov\.cn/article/sj/tjbz/a/[0-9]*/[0-9]*/[0-9]*\.html/
  8. // @match http://www.mca.gov.cn/article/sj/tjbz/a/*/*/*.html
  9. // @grant GM.setClipboard
  10. // @require http://code.jquery.com/jquery-1.11.0.min.js
  11. // ==/UserScript==
  12. $(function () {
  13. 'use strict';
  14.  
  15. String.prototype.endWith = function (endStr) {
  16. if(endStr==='') return true;
  17. var d = this.length - endStr.length;
  18. return (d >= 0 && this.lastIndexOf(endStr) === d);
  19. };
  20.  
  21. /**--------------------------------导出设置-------------------------------------------------*/
  22. var numberType = 4;
  23. var outTextType = '0000';
  24. $('table').before(
  25. $('<div id="jsonBox">' +
  26. '<div style="float:left;margin-right:30px;">' +
  27. '<h4 style="font-weight:700;font-size: 16px;marginTop:10px">代码位数 : </h4>' +
  28. '<label for="twoType" >2位 </label><input type="radio" id="twoType" name="number" value="2" />' +
  29. '<label for="fourType">4位 </label><input type="radio" id="fourType" name="number" value="4" checked="checked" />' +
  30. '<label for="sixType" >6位 </label><input type="radio" id="sixType" name="number" value="6" />' +
  31. '</div>' +
  32. '<div>' +
  33. '<h4 style="font-weight:700;font-size: 16px;marginTop:10px">行政等级 : </h4>' +
  34. '<label for="provinceType">省级 </label><input type="radio" id="provinceType" name="outText" value="0000" checked="checked" />' +
  35. '<label for="cityType" >市级 </label><input type="radio" id="cityType" name="outText" value="00"/>' +
  36. '<label for="countyType" >县级 </label><input type="radio" id="countyType" name="outText" value="" /><br/>' +
  37. '</div>' +
  38. '<div style="margin-top:24px;">' +
  39. '<div style="border:1px solid #b7bbbf;box-sizing:border-box;border-radius:2px;">' +
  40. '<textarea id="jsonArea" style="width:97%;min-height:100px;padding:8px;color:#555;resize:none;line-height:18px;"></textarea>' +
  41. '</div>' +
  42. '</div>')
  43. );
  44. $('input:radio').css('margin', 'auto 50px auto 3px');//设置单选框
  45. $('input:radio[name=number]' ).change(function () { numberType = this.value; textAreaChange(); });
  46. $('input:radio[name=outText]').change(function () { outTextType = this.value; textAreaChange(); });
  47. textAreaChange();
  48.  
  49. function textAreaChange() {
  50. var testArea = getJson(numberType, outTextType);
  51. GM.setClipboard(testArea);
  52. $('#jsonArea').text(testArea);
  53. }
  54. /**--------------------------------导出设置-------------------------------------------------*/
  55.  
  56.  
  57. /**--------------------------------格式化用以显示---------------------------------*/
  58. function getJson(numberType, outTextType) {
  59. var result = '{\n';
  60. $('table tbody tr').each(function () {
  61. var $this = $(this);
  62. if(parseInt($this.attr('height')) !== 19){
  63. return;
  64. }
  65.  
  66. var $td = $this.children();
  67. var code = $td.eq(1).text();
  68. var city = $td.eq(2).text();
  69. if (!!code && code.endWith(outTextType)) {
  70. result += '\'' + code.substring(0, numberType) + '\' : \'' + city + '\',\n';
  71. }
  72. });
  73. result = result.substring(0, result.length-2);
  74. result += '\n}';
  75. return result;
  76. }
  77.  
  78. /**--------------------------------格式化用以显示---------------------------------*/
  79. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址