programmersFunction.js
2.18 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const puppeteer = require('puppeteer')
const cheerio = require('cheerio')
const sanitizeHtml =require('sanitize-html')
const splitData = (string)=>{
const temp = /(<a([^>]+)>)/g.exec(string)
const temp2 = temp[0].split('"');
url = "https://programmers.co.kr/job" + temp2[1];
result = string.replace(/(<([^>]+)>)*(\\t)?/gi, "").replace(/ /g, "").split("\n").filter((ele)=> ele != '')
result.pop()
result.push(url)
return result;
}
const makeObject = (array)=>{
const result = []
let tempData = null
for(let i of array){
tempData = splitData(i)
result.push({
title : tempData[0],
tags : [],
url : tempData[tempData.length - 1],
companyName : tempData[1]
})
for(let j = 6; j < tempData.length - 1; j++){
result[result.length -1].tags.push(tempData[j])
}
}
return result
}
const moveNextPage = async (page)=>{
await page.click('#paginate > nav > ul > li.next.next_page.page-item > a').catch((error)=>{
})
await page.waitForTimeout(300)
return await page.content()
}
const getData = async ()=>{
const browser = await puppeteer.launch();
const page = await browser.newPage();
let result = []
let temp = ""
await page.goto('https://programmers.co.kr/job')
let content = await page.content()
while(true){
if(temp == content){
console.log("finish", result.length)
break;
}
let $ = cheerio.load(content, {decodeEntities: true})
let item = ""
let resArr =[]
for(let i = 1; i <= 20; i++){
selector = `#list-positions-wrapper > ul > li:nth-child(${i})`
item = sanitizeHtml($(selector), {
parser : {
decodeEntities: true
}
})
if(item =='') break;
item = item.split("</div>`")
resArr.push(item[0])
}
result = result.concat(await makeObject(resArr))
resArr = []
temp = content
content = await moveNextPage(page)
}
return result
}
module.exports = {
getData : getData
}
getData()