From a29ea550855cba4d0db77ab08f0d0f866b72119c Mon Sep 17 00:00:00 2001 From: rsgltzyd Date: Thu, 12 Sep 2024 22:52:05 +0800 Subject: [PATCH] add injectionCookie fix --- example.js | 15 --------------- examples/zhihu.js | 26 ++++++++++++++++++++++++++ uselogin.js | 15 --------------- utils/userUtils.js | 1 - 4 files changed, 26 insertions(+), 31 deletions(-) delete mode 100644 example.js create mode 100644 examples/zhihu.js delete mode 100644 uselogin.js diff --git a/example.js b/example.js deleted file mode 100644 index a61e733..0000000 --- a/example.js +++ /dev/null @@ -1,15 +0,0 @@ -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(); - - // 注入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/examples/zhihu.js b/examples/zhihu.js new file mode 100644 index 0000000..6c52473 --- /dev/null +++ b/examples/zhihu.js @@ -0,0 +1,26 @@ +import { launch } from 'puppeteer'; +import 'dotenv/config.js' +import { injectionCookie } from '../utils/userUtils.js'; + +(async () => { + const browser = await launch({ headless: false, args: [`--window-size=1920,1080`], defaultViewport: { width: 1920, height: 1080 } }); + const page = await browser.newPage(); + + // 注入cookie + const webUrl = 'https://www.zhihu.com/hot'; + const cookieFileUrl = './data/zhihu.json'; + await injectionCookie(page, webUrl, cookieFileUrl); + + const hotItems = await page.$$('.HotList-list .HotItem'); + // console.log(hotItems); + console.log("========"); + for (const hot in hotItems) { + if (Object.hasOwnProperty.call(hotItems, hot)) { + const element = hotItems[hot]; + const textContent = await page.evaluate(node => node.textContent.trim(), element); + console.log(textContent); + } + } + + // await browser.close(); +})(); \ No newline at end of file diff --git a/uselogin.js b/uselogin.js deleted file mode 100644 index 51b54d1..0000000 --- a/uselogin.js +++ /dev/null @@ -1,15 +0,0 @@ -import { launch } from 'puppeteer'; -import fs from 'fs'; -import 'dotenv/config.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(); - - 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/userUtils.js b/utils/userUtils.js index 9351ed6..6347fc2 100644 --- a/utils/userUtils.js +++ b/utils/userUtils.js @@ -30,7 +30,6 @@ 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) {