【NodeJs】爬取逆战所有武器
Result
需要的同学可以直接查看结果
下载地址:点击下载
Start
pnpm init
pnpm install cheerio exceljs
Code
import Excel from "exceljs";
import cheerio from 'cheerio'
import https from 'https'
import vm from "vm";
const httpsPromise = (options, postData) => {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let body = '';
res.on('data', (result) => {
body += result;
});
res.on('end', () => {
resolve(body)
});
});
req.on('error', (e) => {
reject(e)
});
req.write(postData);
req.end();
})
}
const start = async () => {
const options = {
hostname: 'mwegame.qq.com',
port: 443,
path: '/nz/weapon/weapon_compare/101168',
method: 'GET',
headers: {
'Referer': 'https://mwegame.qq.com/nz/weapon/weapon_compare/101168',
'Origin': 'https://mwegame.qq.com',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1'
}
}
const data = await httpsPromise(options, '')
const $ = cheerio.load(data)
const code = $('body > script:last-child').text().replace(/Compare\.iGunId[\s\S]*}\);/, '').replace(/seajs\.use[\s\S]*?\{/, 'var Compare = {};')
const script = new vm.Script(code)
const sandbox = {}
script.runInNewContext(sandbox)
const {Compare: {all, cache}} = sandbox
const result = all.sort((a, b) => a['iGunId'] - b['iGunId'])
const workbook = new Excel.stream.xlsx.WorkbookWriter({
filename: './weapon.xlsx',
useStyles: true,
useSharedStrings: true
})
const worksheet = workbook.addWorksheet('sheet1');
for (const item of result) {
worksheet.addRow([
item.szGunName, // 名称
item.iGunId, // id
item.szArmsCate, // 类型
item.szShelf,
item.szGpirce, // 价格
(new Date(item.szShelfTime * 1000)).toLocaleString(), // 上架时间
item.szImgCover ? `https://shp.qpic.cn/tgos/${item.szImgCover.replace(".","/")}/0` : '',
item.szGunDesc // 描述
])
}
worksheet.commit()
await workbook.commit()
}
start()
Usage
node ./上面的js文件
为之则易,不为则难。