共计 1291 个字符,预计需要花费 4 分钟才能阅读完成。
作用是抓取掌阅书城里男频女频各分类的已完结书籍信息。
前面的 pids 和 cids 这两个常数数组都是事先在页面上查看超链接收集的。
这个页面没有任何反爬措施,适合作为简单例子。
const fs = require("fs")
const puppeteer = require('puppeteer');
const url = "http://www.ireader.com/index.php?ca=booksort.index&pca=booksort.index&pid=$pid&order=score&status=3&cid=$cid&page=$page"
const pids = [10, 68]; // 男频,女频
const cids = [[11, 27, 19, 22, 16, 39, 42, 50, 54, 57, 60], [69, 74, 82, 86, 89, 90, 91, 723]]; // 频道中的分类 ID
(async () => {
const browser = await puppeteer.launch({
// headless: false,
ignoreDefaultArgs: ["--enable-automation"],
});
const page = await browser.newPage();
const f = () => {return Array.from($('.bookMation')).map(e => {const id = $('h3 a', e).attr('href').match(/bid=(\d+)/)[1]
const title = $('h3 a', e).text()
const author = $('p.tryread', e).text().replace('试读', '').trim()
const desc = $('p.introduce', e).text()
return {id, title, author, desc}
})
}
let result = [];
for (const i in pids) {const pid = pids[i]
for (cid of cids[i]) {for (let pg = 1; pg < 4; pg++) { // 只抓前三页
const u = url.replace("$cid", cid).replace("$pid", pid).replace("$page", pg)
await page.goto(u);
const res = await page.evaluate("(" + f + ")()")
res.forEach(e => { e.cid = cid; e.pid = pid})
result = result.concat(res)
console.log("page" + pg + "done")
}
console.log("cid" + cid + "done")
}
console.log("pid" + pid + "done")
}
fs.writeFileSync("d:/tmp/ireader_hot.json", JSON.stringify(result), {encoding: "utf-8"})
console.log("all done")
await browser.close();})();
正文完
发表至: javascript
2019-09-17