crawlerOpponent.js
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const puppeteer=require('puppeteer');
const cheerio=require('cheerio');
exports.getDecks=(opponentClass)=>{
const getContent=()=>{
return new Promise((resolve,reject)=>{
const asyncFunc=async ()=>{
const browser=await puppeteer.launch()
try{
const page=await browser.newPage()
await page.setViewport({width:1366,height:768})
await page.goto(`https://hsreplay.net/decks/#timeRange=LAST_30_DAYS&playerClasses=${opponentClass}?hl=ko`,{waitUntil: 'networkidle2'})
const content=await page.content()
browser.close()
return content
}
catch(err)
{
console.log(err)
browser.close()
}
}
resolve(asyncFunc())
})
}
const getDeckInfo=(content)=>{
return new Promise((resolve,reject)=>{
const $=cheerio.load(content)
let deckNames=$('.deck-name')
let deckGames=$('.game-count')
let decks=[]
for(let i=0;i<3;i++){
let deckName=$(deckNames[i]).text()
let deckGame=$(deckGames[i]).text()
decks.push({deckTitle:deckName,deckGame:deckGame})
}
console.log(decks)
resolve(decks)
})
}
getContent()
.then(getDeckInfo)
}