1 Star 0 Fork 1

YooYooY/maotai_crawl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
YooYooY 提交于 2021-01-09 21:35 . init
const path = require("path")
const exceljs = require("exceljs")
const request = require("request")
const cheerio = require("cheerio")
const searchKey = "净利润(扣除非经常性损益后)(万元)"
const crawlUrl = "http://quotes.money.163.com/f10/zycwzb_600519.html#01c01"
const getBody = () =>
new Promise((resolve, reject) => {
request(crawlUrl, (err, res, body) => {
if (err || res.statusCode !== 200) reject(err || `请检查网络,statusCode:${res.statusCode}`)
resolve(body)
})
})
const getData = ($, el) => (index) => {
const data = []
el.eq(index)
.children()
.each(function () {
data.push($(this).text())
})
return data
}
const insertExecl = (columns, data) => {
let workbook = new exceljs.Workbook()
workbook.created = new Date()
workbook.modified = new Date()
workbook.creator = "chenwl"
workbook.lastModifiedBy = "chenwl"
let writeNam = `${searchKey}.xlsx`;
let sheet = workbook.addWorksheet("searchKey")
sheet.columns = columns
sheet.addRows(data)
workbook.xlsx.writeFile(path.join(__dirname, writeNam)).then(() => {
console.log("数据写入完毕 => %s", writeNam)
})
}
;(async () => {
const body = await getBody()
const $ = cheerio.load(body)
const index = $(`td:contains('${searchKey}')`).parent().index()
const $elTr = $(".scr_table tr")
const loadData = getData($, $elTr)
// 日期
let dates = loadData(0)
// 数据
let data = loadData(index + 1)
const insertData = dates.map((date, i) => ({ date, name: data[i] }))
const columns = [
{ header: "日期", key: "date", width: 20 },
{ header: searchKey, key: "name", with: 120 },
]
insertExecl(columns, insertData);
})()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/YooYooY/maotai_crawl.git
git@gitee.com:YooYooY/maotai_crawl.git
YooYooY
maotai_crawl
maotai_crawl
master

搜索帮助