利用puppeteer 公眾號(hào)自動(dòng)導(dǎo)入回復(fù)信息

準(zhǔn)備工作

首先得申請(qǐng)一個(gè)微信公眾號(hào),然后用瀏覽器登錄之后,來到關(guān)鍵詞回復(fù)頁面。利用cookie獲取工具導(dǎo)出當(dāng)前的cookie。

點(diǎn)擊導(dǎo)出當(dāng)前domain的所有cookie

導(dǎo)出來的cookie是一個(gè)數(shù)組,直接復(fù)制到一個(gè)地方備用即可

環(huán)境構(gòu)建

使用全局代理,利用npm下載puppeteer

npm i puppeteer@3.0.0

由于下載puppeteer的時(shí)候會(huì)下載chromium,所以需要全局代理,不下載chromium也可以直接下載puppeteer-core

npm i puppeteer-core

整活

打開頁面
let browser = await puppeteer.launch(launchOptions);
let page = await browser.newPage();
for (let i = 0; i < cookies.length; i++) {
    await page.setCookie(cookies[i]);
}
await page.goto(url);
await page.waitForSelector('html');
await page.content();
let title = await page.title();
點(diǎn)擊添加回復(fù)
await page.$$eval('.weui-desktop-layout__extra', clickAddButton);
async function clickAddButton(nodes) {
    let cnt = 0;
    for (let node of nodes) {
        cnt++;
        if (cnt === 1) continue;
        let treeWalker = document.createTreeWalker(
            node,
            NodeFilter.SHOW_ELEMENT,
            {
                acceptNode: function (node2) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            },
            false
        );
        while (treeWalker.nextNode()) {
            let current = treeWalker.currentNode;
            if (current.tagName.toLowerCase() === 'button' && current.innerHTML === '添加回復(fù)') {
                await current.click();
                return true;
            }
        }
    }
    return false;
}
填寫規(guī)則和匹配
await page.type('.weui-desktop-form__input', val.group + '', {delay: 100});
await page.waitFor(1000);
await page.type('[placeholder=輸入關(guān)鍵詞]', val.title.split('-')[0] + '', {delay: 100});
await page.waitFor(1000);

點(diǎn)擊添加文本回復(fù)信息的按鈕

await page.$$eval('.weui-desktop-msg-sender__tab_text', addTxt);
async function addTxt(nodes) {
    for (let node of nodes) {
        await node.click();
        return true;
    }
    return false;
}
在內(nèi)容區(qū)鍵入回復(fù)內(nèi)容
await page.focus('.edit_area');
await page.keyboard.type('aaa:' + val.title.split('-')[0].trim(), {delay: 100});
await page.keyboard.press('Enter');
await page.keyboard.type('bbb:' + val.title.split('-')[1].trim(), {delay: 100});
await page.keyboard.press('Enter');
await page.keyboard.type('ccc:' + val.url, {delay: 100});
await page.keyboard.press('Enter');
await page.keyboard.type('ddd:' + val.code, {delay: 100});
await page.keyboard.press('Enter');
await page.keyboard.type('eee:' + val.id, {delay: 100});
利用xpath點(diǎn)擊按鈕
await page.waitForXPath('//*[@id="app"]/div[3]/div[2]/div[2]/form/div[3]/div/div/div[3]/div[1]/div/div[3]/button[1]');
let elHandles = await page.$x('//*[@id="app"]/div[3]/div[2]/div[2]/form/div[3]/div/div/div[3]/div[1]/div/div[3]/button[1]');
for (let handle of elHandles) {
    handle.click({delay: 100});
}
利用xpath 點(diǎn)擊保存按鈕
await page.waitForXPath('//*[@id="app"]/div[3]/div[2]/div[2]/div/button[1]');
let elHandle1s = await page.$x('//*[@id="app"]/div[3]/div[2]/div[2]/div/button[1]');
for (let handle of elHandle1s) {
    handle.click({delay: 100});
}

就搞定了

炒雞辣雞原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明來源

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容