From 7ff9cc663fe5d717a881a263cc3e13a0424e4b80 Mon Sep 17 00:00:00 2001 From: rsgltzyd Date: Tue, 30 Jul 2024 20:58:55 +0800 Subject: [PATCH] fix --- .gitignore | 1 + data/zhihu.json | 17 ----------------- example.js | 33 +++++---------------------------- uselogin.js | 34 ++++++---------------------------- utils/businessDataUtil.js | 36 ------------------------------------ utils/userUtils.js | 17 +++++++++++++++++ 6 files changed, 29 insertions(+), 109 deletions(-) delete mode 100644 data/zhihu.json delete mode 100644 utils/businessDataUtil.js diff --git a/.gitignore b/.gitignore index ebf6dfa..d8e98cd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ *.png businessData.json data/user.json +data/zhihu.json diff --git a/data/zhihu.json b/data/zhihu.json deleted file mode 100644 index 754506b..0000000 --- a/data/zhihu.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "_xsrf": "DbVKUPkNSllVquHSGYYzhSxVfoN7Jv3y", - "_zap": "a93b33b3-5012-4fd9-896f-255399bab405", - "d_c0": "AEASegND_hiPTiUjKXU9EemEsTqgkQY453I", - "q_c1": "80150a2bf62e448399ddc5a9ad2fc96f|1722160079000|1722160079000", - "__zse_ck": "001_7B93TpL6TWwuu2z/poOWqUavUfxDbgsj77T8XUma+QyB8h0eUaQadkTFioz5mB5mtErIJbefbPdYH", - "Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49": "1720611937,1722164301,1722254161,1722258218", - "HMACCOUNT": "F0E98F57775BDBB4", - "__snaker__id": "USsgt4pZsujP2PgG", - "gdxidpyhxdE": "knS%2B%2BOKbPWfLNSgDUJMmJY7JxxGDy0jcaOcD5ep1L6V6esNdpEEeWqhNc16agv%2BwcyPuhp4CPnzejgUSAMGm5yC1gZHkDt4atQdjYAjJZVUx%5CRlUrRzPxWiE3BlG3t%2Fs7xAJ%5C2TW1OR7oiM%2F4PvwKwiI4oj9P6ALxKaDrTRyPZhNHeiT%3A1722259465069", - "SESSIONID": "jbNinEEfFAY9yMN2vgpUPblqsLKGEm5IYKl1Lt2ripF", - "JOID": "V10WB0Jst9Ts_0beWmxLSndHLzlKBtOdiJ0DnTsm9ovSiweQbk0heYvzQN9QEPqH8CAwbxbw5C7iCfEwV9Kj-2o", - "osd": "VlkTAktts9Hp9kfaX2lCS3NCKjBLAtaYgZwHmD4v94_Xjg6RakgkcIr3RdpZEf6C9SkxaxP17S_mDPQ5Vtam_mM", - "tst": "h", - "Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49": "1722259680", - "BEC": "32377ec81629ec05d48c98f32428ae46" -} \ No newline at end of file diff --git a/example.js b/example.js index 753d4bb..a61e733 100644 --- a/example.js +++ b/example.js @@ -1,38 +1,15 @@ import { launch } from 'puppeteer'; import 'dotenv/config.js' +import { injectionCookie } from './utils/userUtils'; (async () => { const browser = await launch({ headless: true, args: [`--window-size=1920,1080`], defaultViewport: { width: 1920, height: 1080 } }); const page = await browser.newPage(); - await page.setViewport({ width: 1920, height: 1080 }); - const dev = 'https://dev-dmp.meiguanjia.net/login'; - const stging = 'https://boss.aizhb.net/login'; - let current = ''; - if (process.env.PROFILE === 'dev') { - current = dev; - } else if (process.env.PROFILE === 'stging') { - current = stging; - } - console.log(current); - console.log(process.env.ACCOUNT); - await page.goto(current); - await page.locator('#userName input').fill(process.env.ACCOUNT); - await page.locator('.forgot_box').click(); - await page.waitForSelector('.modify_main'); - await page.locator('#digitalCode input').fill('1'); - await page.locator('#smsCode input').fill(process.env.SMS); - await page.locator('#newPassWord input').fill(process.env.PASSWORD); - await page.locator('#confirmPassWord input').fill(process.env.PASSWORD); - await page.locator('[type="submit"]').click(); - await page.waitForSelector('.arco-message .arco-message-content', { visible: true }); - const arcoMessage = await page.$eval('.arco-message .arco-message-content', node => node.innerHTML); - await page.screenshot({ path: 'resetPassword.png' }); - console.log(arcoMessage); - - page.on('console', msg => { - console.log('PAGE:', msg.text()) - }) + // 注入cookie + const webUrl = 'https://www.zhihu.com/hot'; + const cookieFileUrl = './data/zhihu.json'; + await injectionCookie(page, webUrl, cookieFileUrl); await browser.close(); })(); \ No newline at end of file diff --git a/uselogin.js b/uselogin.js index 6fb170a..51b54d1 100644 --- a/uselogin.js +++ b/uselogin.js @@ -1,37 +1,15 @@ import { launch } from 'puppeteer'; import fs from 'fs'; import 'dotenv/config.js'; - -import { setLocalStorage } from './utils/userUtils.js'; +import { injectionCookie } from './utils/userUtils.js'; (async () => { const browser = await launch({ headless: false, devtools: true, args: [`--window-size=1920,1080`], defaultViewport: { width: 1920, height: 1080 } }); const page = await browser.newPage(); - await page.goto('https://www.zhihu.com/hot'); - const outputLocalStorge = fs.readFileSync('./data/zhihu.json', 'utf-8'); - let localStorageTest = JSON.parse(outputLocalStorge); - let tmpcookies = await page.cookies(); - console.log(tmpcookies); - await page.deleteCookie(...tmpcookies); - - const cookies = new Array(); - for (const iterator in localStorageTest) { - cookies.push({ - name: iterator, - value: localStorageTest[iterator], - domain: '.zhihu.com', - path: '/', - httpOnly: true, - secure: true - }); - console.log(localStorageTest[iterator]); - } - - console.log(cookies); - - await page.setCookie(...cookies); - - await page.goto('https://www.zhihu.com/hot'); - // await browser.close(); + const webUrl = 'https://www.zhihu.com/hot'; + const cookieFileUrl = './data/zhihu.json'; + await injectionCookie(page, webUrl, cookieFileUrl); + + // browser.close(); })(); \ No newline at end of file diff --git a/utils/businessDataUtil.js b/utils/businessDataUtil.js deleted file mode 100644 index fa96d75..0000000 --- a/utils/businessDataUtil.js +++ /dev/null @@ -1,36 +0,0 @@ -export async function getDate(page, fs, file) { - await new Promise(r => setTimeout(r, 2000)); - await page.waitForSelector('.ai_custome'); - const aiCustome = await page.$$('.ai_custome .ai_custome_item .amount'); - let ai = new Set(); - for (const e of aiCustome) { - let str = await page.evaluate(element => element.textContent, e); - ai.add(str); - } - - let importStorage = '\n' + JSON.stringify(Array.from(ai), null, 2) + '\n'; - fs.appendFileSync(file, importStorage, 'utf-8'); - - const elementHandle = await page.waitForSelector('.bi_warp iframe'); - const frame = await elementHandle.contentFrame(); - await frame.waitForSelector('#label-content'); - const items = await frame.$$('#label-content'); // Use $$ to get all matching elements - let its = new Set(); - if (items.length > 0) { // Check if items array is not empty - for (const e of items) { - let str = await frame.evaluate(element => element.textContent, e); // Use frame.evaluate() here - its.add(str.replace(/\s+/g, ' ')); - } - } else { - console.log('#label-content not found in the iframe'); - } - - importStorage = JSON.stringify(Array.from(its), null, 2) + '\n'; - fs.appendFileSync(file, importStorage, 'utf-8'); -} - -export async function cleanType(page, element, timeContent) { - const time = await page.$(element); - await time.click({ clickCount: 3 }); - await time.type(timeContent, { delay: 100 }); -} \ No newline at end of file diff --git a/utils/userUtils.js b/utils/userUtils.js index db9513d..9351ed6 100644 --- a/utils/userUtils.js +++ b/utils/userUtils.js @@ -19,4 +19,21 @@ export async function setLocalStorage(page, file) { }, localStorageTest); console.log('set success'); +} + +/** + * @param {Page} page + * @param {String} webUrl 网站 + * @param {String} cookieFile cookie地址Url + */ +export async function injectionCookie(page, webUrl, cookieFile) { + try { + await page.goto(webUrl); + const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf-8')); + console.log(cookies); + await page.setCookie(...cookies); + await page.goto(webUrl); + } catch (error) { + console.error(error); + } } \ No newline at end of file