-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (93 loc) · 2.61 KB
/
app.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const puppeteer = require('puppeteer');
const fs = require('fs')
const router = require('./router/router')
const express = require('express')
let app = express()
app.set('view engine', 'html')
app.set('views', `${__dirname}/views`)
app.engine('html', require('express-art-template'))
app.use(express.static('public'))
app.use(router)
const url = 'https://www.zhipin.com/';
//方法一
/* (async () => {
const browser = await puppeteer.launch({
headless: false,
slowMo:500,
executablePath: 'C:/Program Files/Google/Chrome/Application/chrome.exe'
});
const page = await browser.newPage();
let job = ['nodejs', 'java', 'python', 'c++', 'php','golang']
let city = ['c101020100', 'c101010100', 'c101280600', 'c101210100', 'c101280100']
let data = []
for(let c = 0; c < city.length; c++){
for(let j =0; j < job.length; j++){
for(let i = 1; i <= 2; i++){
await page.goto(url +`${city[c]}/?query=${job[j]}&page=${i}`);
await page.waitForSelector('#main > div > div.job-list > ul>li')
let salary = await page.$$eval(
".job-primary .info-primary .primary-box .job-limit .red",
(el) => el.map((h) => h.innerText)
);
data.push({
salary:salary
})
}
data.push({
job:job[j]
})
}
data.push({
city:city[c]
})
}
console.log(data);
await browser.close();
fs.writeFile('data.json',JSON.stringify(data,null,'\t'),(err)=>{
if(err){
console.log(err);
}
})
})(); */
//方法二
(async () => {
const browser = await puppeteer.launch({
headless: false,
slowMo:500,
executablePath: 'C:/Program Files/Google/Chrome/Application/chrome.exe'
});
const page = await browser.newPage();
let data = []
for(let i = 1; i<=4; i++){
await page.goto(url + '?page='+i);
await page.waitForSelector('#main > div > div.job-list > ul>li')
const res = await page.evaluate(()=>{
let $ = window.$
let info = []
let item = $('.job-primary .info-primary .primary-box')
item.each((index,ite)=>{
let job = $(ite).find('a').text()
let area = $(ite).find('.job-area').text()
let salary = $(ite).find('.red').text()
info.push({
job : job,
area : area,
salary : salary
})
})
return info
})
data.push(res)
}
console.log(data);
await browser.close();
fs.writeFile('data.json',JSON.stringify(data,null,'\t'),(err)=>{
if(err){
console.log(err);
}
})
})();
app.listen(3000,()=>{
console.log('Running..............');
})
module.exports = app;