Created
April 13, 2022 15:05
-
-
Save daixianceng/d3c8b5cd18579b4137f4097385fe6d03 to your computer and use it in GitHub Desktop.
A股市场股票数据 - NodeJs接口
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fetch = require('node-fetch-retry'); | |
| const iconv = require('iconv-lite'); | |
| module.exports = async function (list) { | |
| const response = await fetch('https://hq.sinajs.cn/list=' + list, { | |
| method: 'GET', | |
| headers: { | |
| Referer: 'https://finance.sina.com.cn', | |
| }, | |
| retry: 3, | |
| pause: 10000, | |
| }); | |
| const orignal = await response.arrayBuffer(); | |
| const result = await iconv.decode(Buffer.from(orignal), 'gbk'); | |
| return result.split('\n').map(parseHqStr).filter(Boolean); | |
| }; | |
| function parseHqStr(str) { | |
| const result = str.match(/^var hq_str_(\w+)="([^"]+)";?$/); | |
| if (result) { | |
| return [result[1], ...result[2].split(',')]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment