表格数据采集

乱码拜拜

当前为 2020-05-20 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         表格数据采集
// @namespace    http://hbchzz.ch.mnr.gov.cn
// @version      0.0.1
// @description  乱码拜拜
// @author       zhd
// @match        http://hbchzz.ch.mnr.gov.cn/Index/*
// @grant        none
// ==/UserScript==

(async function() {
    'use strict';
  
    var allDatas = []
  
    async function wait(time) {
      return new Promise(resolve => {
        setTimeout(()=> resolve(), time * 1000)
      })
    }
  
    function getTableData() {
      var table = document.querySelector('table')
      var trs = table.querySelectorAll('tr')
      var items = []
      for(var i = 1; i < trs.length; i++) {
        var item = {}
        var tds = trs[i].querySelectorAll('td')
        for(var j = 0; j < tds.length; j ++) {
          item[j] = tds[j].innerText
        }
        items.push(item)
      }
      return items
    }
  
    function getAnchors() {
      let anchors = document.querySelectorAll('a')
      anchors = Array.prototype.slice.call(anchors, 0)

      anchors = anchors.filter(anchor => !isNaN(anchor.innerHTML) || (anchor.children.length > 0 && anchor.children[0].src === "http://hbchzz.ch.mnr.gov.cn/images/PageNavi/moren.gif"))
      if(anchors.length > 10) {
        anchors = anchors.slice(1)
      }
      
      return anchors
    }
  
    function update() {
      allDatas = allDatas.concat(getTableData())
    }
  
    async function updatePage(anchors) {
      for(let a of anchors) {
        a.click()
        await wait(1)
        update()
      }
    }
  
    async function recursive() {
      let anchors = getAnchors()
      if(anchors.length < 10) {
        await updatePage(anchors)
        return
      } else {
        await updatePage(anchors)
        recursive()
      }
    }
  
    async function run() {
      console.log('run')
      
      update()
      await recursive()
      console.log(JSON.stringify(allDatas))
    }
  
    await run()
})();