node爬取数据并解析写入文件

今天,领导让我帮着后台解析一些数据,本以为是很简单的数据处理,后台发给我一个链接,我请求一看就懵逼了,密密麻麻的文字,复制到文件一看,大小70M,而且还不止一个链接,没办法,只能写一个node请求数据再解析写入文件给后台。 话不多说,上代码。

var http = require("http");
var fs = require("fs");
const tableNames = ['GEOPDDISULINE','GEOPDLINE','GEOPDRIUN','GEOPDRIUNLINE','GEOPTCOND','GEOPTLINE','GEOPTLINE_ORIG']
const primaryKeys = ['FGUID','OID','FGUID','FGUID','OID','FGUID','OID']
const tableName = tableNames[1]
const primaryKey = primaryKeys[1]

const options = {
    hostname: '192.168.1.152',
    port: 80,
    path: `/dldata/shape/getByTableName?tableName=${tableName}&primaryKey=${primaryKey}`,
    method: 'GET'
}
var htmlData = ""
var req = http.request(options, function (res) {
    res.on("data", function (chunk) {
        htmlData += chunk;
    });
    res.on("end", function () {
        let obj = JSON.parse(htmlData)
        console.log(obj);
        let targetObj = {}
        targetObj.type = "FeatureCollection"
        targetObj.features = obj[0].map((item,index)=>{
            let o = {}
            o.type = 'Feature'
            o.properties = {}
            let itemKeys = Object.keys(item)
            itemKeys.forEach(val=>{
                val !='wktstr' && (o.properties[val] = item[val]==null?'':item[val])
                if(val == 'wktstr'){
                    o.geometry = {}
                    o.geometry.type = item.wktstr.indexOf('MULTILINESTRING')>-1?'MULTILINESTRING':'LINESTRING'
                    o.geometry.coordinates = []
                    let currPolygonData = []
                    if(item.wktstr.indexOf('MULTILINESTRING')>-1){
                        currPolygonData = item.wktstr.match(/\((.+)\)/)[1].split(', (')
                        currPolygonData.forEach((v,i)=>{
                            v = v.replace(/(\(|\))/g,'')
                            v = v.split(', ')
                            let arr = []
                            v.forEach((val,ind)=>{
                                let list = []
                                list[0] = +val.split(' ')[0]
                                list[1] = +val.split(' ')[1]
                                arr[ind] = list
                            })
                            o.geometry.coordinates.push(arr)
                        })
                    }else{
                        currPolygonData = item.wktstr.match(/\((.+)\)/)[1].split(', ')
                        currPolygonData.length==1&&(currPolygonData[1] = currPolygonData[0])
                        currPolygonData.forEach((v,i)=>{
                            let arr = []
                            let [x,y] = v.split(' ')
                            arr[0] = +x
                            arr[1] = +y
                            o.geometry.coordinates.push(arr)
                        })                    
                    }
                }
            })
            return o
        })
        fs.writeFile(`./${tableName}.txt`, JSON.stringify(targetObj), (err)=>{
            console.log(err);
        })
    });
});
req.end();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值