【发布时间】:2019-03-18 14:29:01
【问题描述】:
我创建了一个脚本,使用node.js 和puppeteer 从网站上抓取一些表格数据。虽然我追求的数据不是动态生成的,但我还是用了puppeteer。
但是,当我执行脚本时,我会在单个列中而不是列表中获得输出。此外,只有名称被解析,没有其他内容。我在下面举了两个例子来说明我的意思。
这是我迄今为止尝试过的:
const puppeteer = require("puppeteer");
(async function main() {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://fantasy.premierleague.com/player-list/");
page.waitForSelector("table.ism-table");
const data = await page.$$("table.ism-table tbody tr");
for (const td of data) {
const tdata = await td.$eval("td", item => item.innerText);
console.log(tdata);
}
browser.close();
} catch (e) {
console.log("Here goes the error ", e);
}
})();
我目前的输出:
De Gea
Ederson
Alisson
Kepa
Lloris
Cech
Schmeichel
Grant
我期待的输出:
['De Gea', 'Man Utd', '23', '£5.9']
['Ederson', 'Man City', '43', '£5.7']
['Alisson', 'Liverpool', '39', '£5.6']
['Kepa', 'Chelsea', '36', '£5.5']
['Lloris', 'Spurs', '20', '£5.4']
【问题讨论】:
标签: javascript node.js web-scraping html-table puppeteer