Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyoloro committed Feb 10, 2017
1 parent 725b2da commit c9b5e50
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.idea/
63 changes: 63 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

const cheerio = require('cheerio')
const rp = require('request-promise')
const co = require('co')
const fs = require('fs')
const iconv = require('iconv-lite')

const file = fs.readFileSync('./stock_code.txt', 'utf8')
const SINA_STOCKS_API = 'http://hq.sinajs.cn/list='
const stockCodes = file.split(';')
const cycles = stockCodes.length

const sleep = () => {
let pr = new Promise((resolve, reject) => {
setTimeout(resolve, 1000)
})
return pr
}

co(function* () {
// 初始化stocks内容
const page = yield rp.get('http://quote.eastmoney.com/stocklist.html')
const $ = cheerio.load(page)

const dom = $('#quotesearch > ul').find('a')
const arr = Array.from(dom)
const stockCodeList = arr.map(i => {
const n = i.attribs.href.split('/')
const code = n[n.length - 1].split('.html')[0]
return code
})
fs.writeFileSync('./stock_code.txt', stockCodeList.join(';'))
console.log(`完成初始化stocks ... 当前${stockCodeList.length}支 ... 等待1s`)

yield sleep()

// 持续查询
for (let n = 0; n <= cycles - 1; n++) {
let buffer = yield rp({
method: 'GET',
uri: SINA_STOCKS_API + stockCodes[n],
encoding: null
})
let decode = iconv.decode(buffer, 'GBK')
console.log(decode)
yield sleep()
}
})
.catch(e => console.error(e))

// // ---- 注意计算过程中api挂掉的情况 ----
// // ---- 个体查询 ------
// co(function* () {
// let buffer = yield rp({
// method: 'GET',
// uri: SINA_STOCKS_API + 'sh502003',
// encoding: null
// })
// let decode = iconv.decode(buffer, 'GBK')
// console.log(decode)
// })
// .catch(e => console.error(e))
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "stocks-list",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^0.22.0",
"co": "^4.6.0",
"iconv-lite": "^0.4.15",
"request": "^2.79.0",
"request-promise": "^4.1.1"
}
}
1 change: 1 addition & 0 deletions stock_code.txt

Large diffs are not rendered by default.

0 comments on commit c9b5e50

Please sign in to comment.