推荐使用cheerio, api 接口简单易于使用
const $ = require('cheerio')
const {JSDOM} = require("jsdom");
const axios = require('axios')
async function getHtml(url) {
return (await axios.get(url)).data
}
async function jsdom(html) {
const dom = new JSDOM(html);
let links = dom.window.document.querySelectorAll("a.link-button")
// for (let i of [...links])
// console.log(i.text)
return [...links].map(i => i.text)
}
async function cheerio(html) {
let dom = $.load(html)
let links = dom('a.link-button')
return links.map(
(i, el) => {
// console.log(i, $(el).text())
return $(el).text()
}
)
}
async function start() {
const url = 'http://daily.zhihu.com/'
let html = await getHtml(url)
let a1 = await jsdom(html)
let a2 = await cheerio(html)
console.log(a1.length, a2.length)
for (let i = 0; i < a1.length; i++) {
console.log(a1[i], '---', a2[i])
}
}
start()