const puppeteer = require('puppeteer')
const path = require('path')
puppeteer.launch({
headless: true,
args: [ // 禁用一些功能
'--no-sandbox', // 沙盒模式
'--disable-setuid-sandbox', // uid沙盒
'--disable-dev-shm-usage', // 创建临时文件共享内存
'--disable-accelerated-2d-canvas', // canvas渲染
'--disable-gpu' // GPU硬件加速
],
ignoreDefaultArgs: ["--enable-automation"]
}).then(async browser => {
const page = await browser.newPage()
page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36')
await page.goto('http://hq.zgw.com/huizong/jiancai.html')
sleep(2000)
const time = await page.$eval('.lm_list > ul:nth-child(2) > li:nth-child(1) > span', a => a.innerHTML)
console.log(getToday())
if (time === getToday()) {
const url = await page.$eval('.fl.lm_left >.lm_list > ul:nth-child(2) > li:nth-child(1) > a', a => a.href)
await page.goto(url)
sleep(2000)
let clip = await page.evaluate(() => {
let {
x,
y,
width,
height
} = document.querySelector('.neirong > table').getBoundingClientRect()
return {
x,
y,
width,
height
}
})
const title = await page.$eval('body > div.wrap > div.cslm_tit > div.hq_con > div.fl.lm_left > div > div.lm_m > div.lm_mt > div.time', a => {
return a.innerHTML
})
const date = title.match(/\d+-\d+-\d+/)
await page.screenshot({
path: `${date}.png`,
clip: clip
})
save(path.resolve(`${date}.png`), `${date}`)
}
await browser.close()
}).then(() => {
}).catch(e => {
console.log(e)
})
/**
* 获取今日日期字符串
*/
function getToday () {
const month = new Date().getMonth() + 1
const day = new Date().getDate()
let today = ''
if (month < 10) {
today += `0${month}`
} else {
today += month + ''
}
if (day < 10) {
today += `-0${day}`
} else {
today += `-${day}`
}
return today
}
/**
* 延时
* @param {Number} time
*/
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), time)
})
}
/**
* 上传截图
* @param {String} path 文件路径
* @param {String} name 文件名称
*/
function save (path, name) {
const qiniu = require('qiniu')
const accessKey = ''
const secretKey = ''
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const options = {
scope: 'zzestlgc',
returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
}
const putPolicy = new qiniu.rs.PutPolicy(options)
const uploadToken=putPolicy.uploadToken(mac)
const config = new qiniu.conf.Config()
// 空间对应的机房
config.zone = qiniu.zone.Zone_z2
const formUploader = new qiniu.form_up.FormUploader(config)
const putExtra = new qiniu.form_up.PutExtra()
const key = name
formUploader.putFile(uploadToken, key, path, putExtra, (err, body, info) => {
if (err) throw err
if (info.statusCode === 200) {
console.log(body)
} else {
console.log(info.statusCode)
console.log(body)
}
})
}