我正在尝试使用 Node.js 从 here 中抓取数据,但是,当我使用 node.js 运行 js 文件时,没有任何显示。
我尝试将此代码与维基百科表格一起使用,并且奏效了。 当我在控制台中使用相同的函数时,我确实得到了我需要的列表,但它在 node 中不起作用。
这是我的代码:
const request = require("request-promise");
const cheerio = require("cheerio");
async function main() {
const result = await request.get("https://www.electionguide.org/elections/?inst=&cont=&yr=2021");
const $ = cheerio.load(result);
$("#datagrid > tbody > tr > td > a").each((index, element) => {
console.log($(element).text());
});
}
main();
答案 0 :(得分:0)
数据表主体由加载页面后运行的 javascript 填充,因此您必须先使用 selenium 加载页面,然后在一定的超时时间后进行抓取,或者不是从 html 抓取数据,尝试以某种方式执行 javascript 代码。
php /home/abc/www/myphp.php '¶m1=abc¶m2=def¶m3=123'