代码拉取完成,页面将自动刷新
const axios = require("axios");
const cheerio = require("cheerio");
const xlsx = require("node-xlsx");
const fs = require("fs");
async function init() {
const response = await axios.get(
"http://quotes.money.163.com/f10/zycwzb_600519.html"
);
const $ = cheerio.load(response.data);
let title = $(".limit_sale").html();
const titleArr = [];
const result = [];
const target = "净利润(扣除非经常性损益后)(万元)";
let index = 0,
targetIndex = 0;
title.replace(
/<td\s+class="td_1">([\S\s]*?)<\/td>/gi,
function ($0, $1) {
titleArr.push($1);
result.push([]);
if (target === $1) {
targetIndex = index;
}
index++;
return $0;
},
""
);
const body = $(".scr_table").html();
const bodyArr = [];
const dateArr = [];
index = -1;
body.replace(
/<tr[\S\s]*?>([\S\s]*?)<\/tr>/gi,
function ($0, $1, $2) {
if (index === -1) {
$0.replace(
/<th>([\S\s]*?)<\/th>/gi,
function (s1, s2) {
dateArr.push(s2);
return s1;
},
""
);
}
if (index === targetIndex) {
$0.replace(
/<td>([\S\s]*?)<\/td>/gi,
function (s1, s2) {
bodyArr.push(s2);
return s1;
},
""
);
}
index++;
return $0;
},
""
);
var xlsx = require("node-xlsx");
var fs = require("fs");
var data = [
{
name: "sheet1",
data: [
["报告日期", ...dateArr],
[target, ...bodyArr],
],
},
];
var buffer = xlsx.build(data);
fs.writeFile("./resut.xls", buffer, function (err) {
if (err) throw err;
console.log("Write to xls has finished");
var obj = xlsx.parse("./" + "resut.xls");
console.log(JSON.stringify(obj));
});
}
init();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。