【发布时间】:2019-06-30 23:36:39
【问题描述】:
最后我想出了如何使用 Node.js。安装了所有库/扩展。所以 puppeteer 正在工作,但是就像以前的 Xmlhttp 一样......它只获取页面的模板/正文,没有需要的信息。页面上的所有脚本都会在浏览器(Web 应用程序?)中打开几秒钟后启动。加载整个页面后,我需要在某些标签内获取信息。另外,我会问,是否可以使用纯 JavaScript,因为我不使用类似 jQuery 的代码。所以它对我来说难度增加了一倍......
这是我目前所拥有的。
const puppeteer = require('puppeteer');
const $ = require('cheerio');
let browser;
let page;
const url = "really long link with latitude and attitude";
(async () => puppeteer
.launch()
.then(await function(browser) {
return browser.newPage();
})
.then(await function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(await function(html) {
$('strong', html).each(function() {
console.log($(this).text());
});
})
.catch(function(err) {
//handle error
}))();
我只在强标签内获得模板默认正文元素。但它应该包含比 10 项更多的数据。
【问题讨论】:
-
使用
async/await有点奇怪和then()。通常是const browser = await puppeteer.launch(); const page = await browser.newPage();...等
标签: javascript node.js parsing web-scraping puppeteer