This commit is contained in:
rsgltzyd 2024-09-12 22:52:05 +08:00 committed by wenpeng
parent ece0c181b0
commit ec8a8a6474
6 changed files with 29 additions and 128 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules/
*.png
businessData.json
data/user.json
data/zhihu.json

View File

@ -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"
}

View File

@ -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();
})();

View File

@ -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();
})();

View File

@ -1,55 +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 });
}
export async function changeTimeDimension(page, timeDimension) {
let rb = await page.$('.arco-space-item .arco-select-view-value');
let s = await page.evaluate(node => node.textContent.trim(), rb);
console.log(s);
await rb.click();
const timeMap = new Map();
rb = await page.$$('.arco-select-dropdown-list .arco-select-option');
for (const iterator of rb) {
s = await page.evaluate(node => node.textContent.trim(), iterator)
console.log(s);
timeMap.set(s, iterator);
}
console.log('===');
console.log(timeMap);
await timeMap.get(timeDimension).click();
}

View File

@ -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);
}
}