From f5538401321cadd5418629b2924840779ee4a618 Mon Sep 17 00:00:00 2001 From: rsgltzyd Date: Tue, 30 Jul 2024 22:18:39 +0800 Subject: [PATCH] fix --- .gitignore | 1 + examples/zhihu.js | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d8e98cd..72c9fe7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ businessData.json data/user.json data/zhihu.json +data/zhihuData.json diff --git a/examples/zhihu.js b/examples/zhihu.js index 6c52473..6407a11 100644 --- a/examples/zhihu.js +++ b/examples/zhihu.js @@ -1,6 +1,7 @@ import { launch } from 'puppeteer'; import 'dotenv/config.js' import { injectionCookie } from '../utils/userUtils.js'; +import fs from 'fs'; (async () => { const browser = await launch({ headless: false, args: [`--window-size=1920,1080`], defaultViewport: { width: 1920, height: 1080 } }); @@ -12,15 +13,26 @@ import { injectionCookie } from '../utils/userUtils.js'; await injectionCookie(page, webUrl, cookieFileUrl); const hotItems = await page.$$('.HotList-list .HotItem'); - // console.log(hotItems); console.log("========"); + + const hotText = new Array(); + 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); + + const h2Element = await element.$('h2'); + const pElement = await element.$('p'); + + const title = h2Element != null ? await h2Element.evaluate(e => e.textContent) : ''; + const excerpt = pElement != null ? await pElement.evaluate(e => e.textContent) : ''; + + hotText.push({ title, excerpt }); } } + console.log("end..."); - // await browser.close(); + fs.writeFileSync('./data/zhihuData.json', JSON.stringify(hotText, null, 2)); + + await browser.close(); })(); \ No newline at end of file