腾讯云TCB云函数抓取微信订阅号话题标签文章

2020-11-19 18:17:26

本文记录如何使用云函数实现微信公众号话题标签文章列表的抓取

# 实验准备

获取订阅号话题标签链接

# 核心代码

需求是获取文章的标题、跳转链接、发布时间及文章配图的缩略图就可以了,具体代码如下。


'use strict';
const puppeteer = require('puppeteer')
exports.main = async (event, context) => {
    const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
        dumpio: false,
    })
    const page = await browser.newPage()
    page.setUserAgent(
        'Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro Build/QKQ1.191117.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2581 MMWEBSDK/200801 Mobile Safari/537.36'
    )
    await page.goto('获取到的话题标签链接', {
        waitUntil: 'networkidle0',
    })

    const articleInfo = await page.evaluate(() => {
        const element = document.querySelector(".album__list");
        let items = element.getElementsByClassName("album__list-item");
        let title = [];
        for (let i = 0; i < items.length; i++) {
            let cover = items[i]
                .getElementsByClassName("album__item-img")
                .item(0)
                .getAttribute("style").replace("background-image: url(", "");
            cover = cover.substring(0, cover.length - 2);
            title.push({
                title: items[i].getAttribute("data-title"),
                url: items[i].getAttribute("data-link"),
                date: items[i].getElementsByClassName("album__item-info-item").item(0)
                    .innerHTML,
                pic: cover,
            });
        }

        return title;
    });
    await browser.close();
    return articleInfo
};

# 访问示例地址

http://tcb.xuedingmiao.com/get_wx_articles (opens new window)

# 参考资料

本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 3.0 许可协议。可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。

扫描下方二维码阅读当前文章

浏览器、微信扫码

评 论:

好文推荐
每天进步一点点~