-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (88 loc) · 2.6 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
'use strict'
const puppeteer = require('puppeteer')
const Config = require('./config')
const devices = require('puppeteer/DeviceDescriptors')
// const iPhone6 = devices['iPhone 6']
;(async() => {
const browser = await puppeteer.launch({
//设置超时时间
timeout: 15000,
//如果是访问https页面 此属性会忽略https错误
ignoreHTTPSErrors: true,
// 打开开发者工具, 当此值为true时, headless总为false
devtools: false,
// 关闭headless模式, 会打开浏览器
headless: false
})
const page = await browser.newPage()
// 模拟 iPhone6
// await page.emulate(iPhone6)
await page.goto(Config.url)
console.log('等待中...')
await page.waitFor(2000)
// 获取页面标题
let title = await page.title()
console.log('页面标题为:' + title)
// 获取商品总数
const SELECTOR_PRODUCT_NUM= '.search-result span'
// const SELECTOR_PRODUCT_NUM= '#s_hd_links'
const productNum = await page.$eval(SELECTOR_PRODUCT_NUM, el =>{
return el.innerText
})
console.log('商品总数为:' + productNum)
// 获取商品信息
let productInfo = await page.evaluate(() => {
const SELECTOR_PRODUCT_INFO= 'dl.item'
let el = document.querySelectorAll(SELECTOR_PRODUCT_INFO)
el = Array.prototype.slice.call(el)
return el.map(v => {
const name = v.querySelector('.item-name').innerText
const price = v.querySelector('.c-price').innerText
const saleNum = v.querySelector('.sale-num').innerText
return {
name,
price,
saleNum
}
})
})
console.log('商品信息为:' + productInfo)
// 下一页
let hasNext = true
while(hasNext){
const nextBtn = await page.$('a.next')
if(!nextBtn) {
hasNext = false
return false
}
await nextBtn.click()
await page.waitFor(5000)
// 获取商品信息
let productInfoNext = await page.evaluate(() => {
const SELECTOR_PRODUCT_INFO= 'dl.item'
let el = document.querySelectorAll(SELECTOR_PRODUCT_INFO)
el = Array.prototype.slice.call(el)
return el.map(v => {
const name = v.querySelector('.item-name').innerText
const price = v.querySelector('.c-price').innerText
const saleNum = v.querySelector('.sale-num').innerText
return {
name,
price,
saleNum
}
})
})
productInfo.push(productInfoNext)
await page.screenshot({
path: 'screenshot/demo_'+ new Date().getTime() +'.png',
fullPage: true
})
console.log('总商品信息为:' + productInfo)
}
await page.screenshot({
path: 'screenshot/demo.png',
fullPage: true
})
// await browser.close()
})()