本文是 puppeteer 在云函数中的简单应用,主要功能为爬取网站上最新的微信产品相关信息。数据来源为新榜资讯。
# 程序思路
使用 puppeteer 打开新榜资讯 (opens new window)后点击微信 tab 等待内容加载,获取列表区的微信资讯。
# 核心代码
index.js
'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('https://www.newrank.cn/public/news.html?#', {
waitUntil: 'networkidle0',
})
await page.click('.media-tags-more .row:last-child .float-left span:first-child')
await page.waitFor(200)
const wxInfoList = await page.evaluate(() => {
const elements = Array.from(document.querySelectorAll('.media-main-left-news-list li'))
return elements.map(s => {
let a = s.getElementsByTagName('h3').item(0).getElementsByTagName('a').item(0)
let txt = a.innerHTML
let url = a.getAttribute('href')
return { title: txt, url: url }
})
})
await browser.close()
return {list:wxInfoList}
}
# HTTP访问
HTTP访问服务地址
http://tcb.xuedingmiao.com/wx_news (opens new window)