批量获取这个网站的数据 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}
]]
});
});
}
}
})
}
})
网友评论