node爬取LOL英雄信息
生活随笔
收集整理的這篇文章主要介紹了
node爬取LOL英雄信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
連接mongodb數據庫并插入多個數據
function insertMany(collection,arr){return new Promise((resolve,reject)=>{var MongoClient = require('mongodb').MongoClient;var url = 'mongodb://127.0.0.1:27017/';MongoClient.connect(url,function(err,db){if(err) {throw err}var dbo = db.db('lolinfo');//連接 lolinfo 數據庫dbo.collection(collection).insertMany(arr,function(err,res){if(err) reject(err);console.log("文檔輸入成功 插入文檔數為",res.insertedCount);db.close();resolve();}) })}) }獲取英雄列表
進入官網f12拿取接口
//獲取英雄列表 async function getHeroList(){let httpUrl = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js';let res = await axios.get(httpUrl);console.log(res.data.hero);await insertMany('herolist',res.data.hero);return res.data.hero; }getHeroList()通過heroID獲取單個英雄信息
連接mongodb數據庫并插入一個英雄信息
function insertOne(collection,obj){return new Promise((resolve,reject)=>{const MongoClient = require('mongodb').MongoClientconst url = 'mongodb://127.0.0.1:27017/'MongoClient.connect(url,function(err,db){if(err) {throw err}var dbo = db.db('my');//連接 lolinfo 數據庫dbo.collection(collection).insertOne(obj,function(err,res){if(err) reject(err)console.log("文檔輸入成功");db.close();resolve()}) })}) }插入數據
async function getHero(heroid){let httpUrl = `https://game.gtimg.cn/images/lol/act/img/js/hero/${heroid}.js`let result = await axios.get(httpUrl)await insertOne('heroInfo',result.data.hero)// console.log(result.data.hero);return result.data }async function run(){let herolist = await getHeroList()// console.log(herolist);await herolist.reduce(async (pre,item,index)=>{await prereturn new Promise(async (resolve,reject)=>{await getHero(item.heroId)resolve()})},Promise.resolve()) } run()總結
以上是生活随笔為你收集整理的node爬取LOL英雄信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Css知识回顾
- 下一篇: 百度智能云区块链产品负责人刘尧:Web3