js实现批量获取年度社会责任报告排行数据

批量获取这个网站的数据 http://stockdata.stock.hexun.com/zrbg/Plate.aspx?date=2017-12-31

网站数据

使用Chrome调试工具获取到接口为:http://stockdata.stock.hexun.com/zrbg/data/zrbList.aspx?date=2017-12-31&count=20&pname=20&titType=null&page=1&callback=hxbase_json11545997932143
返回数据格式示例:

hxbase_json1({sum:3578,list:[{Number:'1',StockNameLink:'stock_bg.aspx?code=600816&date=2017-12-31',industry:'安信信托(600816)',stockNumber:'22.38',industryrate:'74.77',Pricelimit:'B',lootingchips:'9.50',Scramble:'9.00',rscramble:'5.20',Strongstock:'28.69',Hstock:' <a href ="http://www.cninfo.com.cn/finalpage/2018-02-08/1204401583.PDF" target="_blank"><img alt="" src="img/table_btn1.gif"></img ></a>',Wstock:'<a href ="http://stockdata.stock.hexun.com/600816.shtml" target="_blank"><img alt="" src="img/icon_02.gif"></img ></a>',Tstock:'<img alt="" onclick="addIStock(\'600816\',\'1\');"  code="" codetype="" " src="img/icon_03.gif"></img >'}])

由于不是标准json格式,不好利用,故对数据进行改造
js的replace()只能替换第一个匹配字符,没有全局替换方法,于是使用正则表达式里的g为全局标志给String类增加一个replaceAll()函数

    String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {  
        if (!RegExp.prototype.isPrototypeOf(reallyDo)) {  
            return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);  
        } else {  
            return this.replace(reallyDo, replaceWith);  
        }  
    }

接下来把原数据进行改造成json格式,把单引号变成双引号,该加的地方加引号

json = json.replaceAll("'","\"");
json = json.replaceAll("{", "{\"");
json = json.replaceAll(":", "\":");
json = json.replaceAll(",", ",\"");
json = json.replaceAll("},\"", "},");
json = json.replace('[object Object]','')
json = json.slice(0,-1);
json = '['+json + ']'

这里采用取巧的办法,使用防xss工具把没用的链接消除

json = filterXSS(json)
json = json.replaceAll('"_blank"','')
var newjson = JSON.parse(json)

nginx配置:

location /zang { 
  proxy_pass   http://stockdata.stock.hexun.com/;
}

前端完整js代码如下:

$(document).ready(function(){
        var json = {};
        for(var i=1;i<=120;i++){
            var url = 'http://localhost:8888/zang/zrbList.aspx?date=2010-12-31&count=20&pname=20&titType=null&page='+i+'&callback=hxbase_json11545820683857';
            $.ajax({
                method:'get',
                url:url,
                async:false,
                success:function(data){
                    data = data.slice(29,-3)
                    data = data + ','
                    console.log(data)
                    json=json + data;
                    console.log(i)
                    if(i==120){
                        console.log(typeof(json))
                        String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {  
                            if (!RegExp.prototype.isPrototypeOf(reallyDo)) {  
                                return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);  
                            } else {  
                                return this.replace(reallyDo, replaceWith);  
                            }  
                        }
                        json = json.replaceAll("'","\"");
                        json = json.replaceAll("{", "{\"");
                        json = json.replaceAll(":", "\":");
                        json = json.replaceAll(",", ",\"");
                        json = json.replaceAll("},\"", "},");
                        json = json.replace('[object Object]','')
                        json = json.slice(0,-1);
                        json = '['+json + ']'
                        json = filterXSS(json)
                        json = json.replaceAll('"_blank"','')
                        var newjson = JSON.parse(json)
                        console.log(newjson)

                       layui.use('table', function(){
                            var table = layui.table;
                            table.render({
                                elem: '#demo'
                                ,data: newjson 
                                ,toolbar: 'default' 
                                ,limit:10000
                                ,cols: [[ 
                                {field: 'Number', title: '序号', width:80, sort: true, fixed: 'left'}
                                ,{field: 'industry', title: '股票名称(代码)', width:80}
                                ,{field: 'Strongstock', title: '股东责任', width:80, sort: true}
                                ,{field: 'stockNumber', title: '股东责任', width:80} 
                                ,{field: 'industryrate', title: '总得分', width: 177}
                                ,{field: 'Pricelimit', title: '等级', width: 80, sort: true}
                                ,{field: 'lootingchips', title: '员工责任', width: 80, sort: true}
                                ,{field: 'Scramble', title: '供应商、客户和消费者权益责任', width: 80}
                                ,{field: 'rscramble', title: '环境责任', width: 135, sort: true}
                                ]]
                            }); 
                          });
                    }
                }
            })
        }
 
    })
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,121评论 2 89
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,981评论 1 45
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 13,104评论 0 3
  • 一连几天的炎热,仿佛我们又只身于夏季,太阳高挂,阳光虽不强烈,只是柔和地照射着。这几天的温度一直很高,这仅仅...
    五行侠阅读 1,683评论 1 0
  • 故事开始以前 最初的那些春天 阳光洒在杨树上 风吹来 闪银光 街道平静而温暖 钟走得好慢 那是我还不识人生之味的年...
    村落时光阅读 825评论 0 0