请求响应监听
javascript
// 把pupp 实例放全局,避免来回传参,出现问题
let browser
//判断调用生产接口
// const url = Boolean(process.env.MSF_ENV === 'docker') ? 'http://47.106.238.92:8000/' : 'http://10.100.3.228:8000/'
const url = 'http://'
module.exports = async function puppshopee(inputs) {
browser = await pupp.launch(config.browserOption)
console.log('进入pupp')
try {
} catch (err) {
console.log(err)
} finally {
console.log('杀浏览器')
await kill_process(browser)
}
}
javascript
// 模拟授权操作
async function newpage(item) {
return new Promise(async(resolve, reject) => {
// 新建页签
const target = await browser.newPage()
try {
// 设置超时
await target.setDefaultNavigationTimeout(20000)
// 写入请求设备信息
await target.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36')
// 设置反爬
await setBrowserPage(target)
// 开启http监听
await target.setRequestInterception(true)
// 拦截所有请求
target.on('request', async req => {
await req.continue()
})
// 监听所有响应
target.on('response', async res => {
if (res.url().includes('https://account.seller.shopee.com/api/account/login/')) {
console.log('-----------登录接口--------------')
console.log(res.status())
if (res.status() === 200) {
// 原本接口返回的数据 {"code":0,"data":"hello ajanuw"} //string类型
// console.log(JSON.parse(await res.text()))
// 登录成功,点击授权页面交互
await clicklarge()
} else if (res.status() === 403) {
// 失败代表账号密码错误,直接关页签
target.close()
resolve({
message: '登录失败',
code: 500,
data: {}
})
} else {
// 未知错误
target.close()
resolve({
message: `登录失败,未知错误`,
code: res.status(),
data: {}
})
}
} else if (res.url().includes('https://subaccount.shopee.com/api/v1/shop/auth_authorize/')) {
console.log('-----------授权接口--------------')
// console.log(res.status()) 为http状态码
if (res.status() === 200) {
// 原本接口返回的数据 {"code":0,"data":"hello ajanuw"} //string类型
// console.log(JSON.parse(await res.text()))
// let resd = JSON.parse(await res.text())
target.close()
resolve({
message: '授权成功',
code: 200,
data: item
})
} else {
target.close()
resolve({
message: `授权失败`,
code: res.status(),
data: {}
})
}
}
})
// 跳转登录页面
await target.goto(item.url, { 'waitUntil': 'networkidle2' })
console.log('进入登录页面')
// 等待密码输入框元素出现
await target.waitForSelector('input[type=password]', { timeout: 60000 })
console.log('密码输入框等到了')
// 等待2秒
// await target.waitForTimeout(3000)
await target.waitFor(500)
// 输入密码,主动触发双向绑定,点登录
await target.evaluate(async(item) => {
document.querySelector('input[autocomplete="new-password"]').value = item.password
document.querySelector('input[autocomplete="new-password"]').dispatchEvent(new Event('input'))
document.querySelector('button[class="shopee-button login-btn shopee-button--primary shopee-button--large shopee-button--block"]').click()
return true
}, item)
console.log('登录按钮已点击')
// 等待2秒
// await target.waitForTimeout(2000)
await target.waitFor(500)
// 等待点击链接出现,时间较长
async function clicklarge() {
try {
await target.waitForSelector('button[class="shopee-button button inlineButton shopee-button--primary shopee-button--x-large"]', { timeout: 40000 })
console.log('授权按钮等到了')
await target.evaluate(() => {
document.querySelector('button[class="shopee-button button inlineButton shopee-button--primary shopee-button--x-large"]').click()
})
} catch (error) {
target.close()
resolve({
message: error,
code: 500,
data: {}
})
}
}
} catch (error) {
console.log(`-----------------授权错误:${error}-------------------------`)
target.close()
resolve({
message: error,
code: 500,
data: {}
})
} finally {
}
})
}
javascript
//设置反爬
setBrowserPage:async (page)=>{
// 设置user_agent
// await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4181.9 Safari/537.36")
// 设置webdriver
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, 'webdriver', { get: () => false })
Object.defineProperty(navigator, 'plugins', {
get: () => [
{
0: { type: 'application/x-google-chrome-pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: Plugin },
description: 'Portable Document Format',
filename: 'internal-pdf-viewer',
length: 1,
name: 'Chrome PDF Plugin'
},
{
0: { type: 'application/pdf', suffixes: 'pdf', description: '', enabledPlugin: Plugin },
description: '',
filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai',
length: 1,
name: 'Chrome PDF Viewer'
},
{
0: { type: 'application/x-nacl', suffixes: '', description: 'Native Client Executable', enabledPlugin: Plugin },
1: { type: 'application/x-pnacl', suffixes: '', description: 'Portable Native Client Executable', enabledPlugin: Plugin },
description: '',
filename: 'internal-nacl-plugin',
length: 2,
name: 'Native Client'
}
]
})
})
await page.evaluateOnNewDocument(() => {
window.navigator.chrome = {
runtime: {},
loadTimes: function() {
},
csi: function() {
},
app: {}
}
window.navigator.language = {
runtime: {},
loadTimes: function() {
},
csi: function() {
},
app: {}
}
})
}
javascript
// 杀死模拟浏览器进程
kill_process: async(browser) => {
if (process.env.MSF_ENV) {
await browser.process().kill('SIGHUP')
} else {
await browser.process().kill()
}
},
javascript
//读json
async function getJSON_password(username) {
return new Promise(((resolve, reject) => {
fs.readFile('./shopee.json', 'utf-8', function(err, data) {
if (err) {
resolve(false)
} else {
let pass = JSON.parse(data).find((item) => {
if (item['name'] === username) {
return item
}
})
if (pass) {
resolve(pass.password)
} else {
resolve('shopee888')
}
}
})
}))
}
// 读文件
async function readFile(username) {
return new Promise((resolve, reject) => {
fs.readFile('./err.txt', 'utf-8', (err, data) => {
if (err) {
resolve(false)
} else {
resolve(data.split('\n').includes(username))
}
})
})
}
// 写文件
async function appendFile(username) {
console.log('进入写文件')
fs.appendFileSync('./err.txt', username + '\n')
}