|
每次请求1只股票的数据,批量请求,会给服务器带来压力,慎用,大量请求,或许会被封IP?
代码参考如下:
- async function 股票人气榜(){
- async function getRes(argm1, gpdm){
- let p = await fetch(`https://emappdata.eastmoney.com/stockrank/${argm1}`, {
- method: 'POST',
- headers: {'content-type': 'application/json'},
- body: JSON.stringify({'srcSecurityCode': `${gpdm}`})
- });
- let res = await p.json();
- return res;
- };
- let t1 = Date.now()
- const gpdms = Range('表1[代码]').Value2.flat().map((x, i) => {
- if (typeof x == 'number') x = String(x);
- if (x.length < 6) x = '0'.repeat(6 - x.length) + x;
- x = '03'.includes(x[0]) ? 'SZ' + x : 'SH' + x;
- return x;
- });
- Range('表1[代码]').Value2 = gpdms.map(x =>
- [`=HYPERLINK("http://guba.eastmoney.com/rank/stock?code=${x}", "${x.slice(2)}")`])
- let res1 = await Promise.all(gpdms.map((x, i) => getRes('getCurrentLatest', x)));
- let res2 = await Promise.all(gpdms.map((x, i) => getRes('getProfileStockRank', x)));
- let res = res1.map((x, i) =>
- [x.data.rank, x.data.rankChange, res2[i].data.oldUidRate, res2[i].data.newUidRate]
- );
- Range('表1[[实时排名]:[铁杆粉丝]]').Value2 = res;
- console.log(`${res.length}只股票,用时:${(Date.now()-t1)*0.001}秒`)
- }
复制代码 附带说一下,求助问题,首先要尽量给答题者减少不必要麻烦,比如,楼主表格中的股票代码,我在全部27行代码中,就用了8行代码清洗。
其次,楼主需要的数据,并不能从楼主给的网址抓取,从我这抓到的包来看,返回的是加密的数据,要另找途径抓包。。。
|
评分
-
1
查看全部评分
-
|