首页 > 解决方案 > Web Scraper 返回 Null 值

问题描述

我正在尝试将数据导入本地应用程序,以获取从篮球参考网站收集的大学篮球统计数据。目前,我有能够收集玩家平均值的代码,并希望将其推断为玩家总数。这是我将用于此示例的 URL:https ://www.sports-reference.com/cbb/players/lindell-wigginton-1.html 。

这是我正在使用的代码的子集:

request(url, (err, res, html) => {
    if (!err && res.statusCode == 200) {

        // Load page HTML
        const $ = cheerio.load(html); 

        /****** Get player Per Game stats ******/ 
        // Push each value to a stats array
        $("#players_per_game td").each((i ,el) => {
            const item = $(el).text();
            stats.push(item);
        });

        /****** Get Player Totals ******/
        $("#players_totals td").each((i ,el) => {
            const item = $(el).text();
            totals.push(item);
        });
     }
});

球员每场比赛的统计数据填充得很好,但球员总数数组仍然为空。这是我在爬虫中通过“players_per_game”id 引用的 Per Game 表的页面 HTML:

<table class="row_summable sortable stats_table now_sortable" id="players_per_game" data-cols-to-freeze="1"><caption>Per Game Table</caption>

这是同一张表,其中刮板中的引用不起作用:

<table class="row_summable sortable stats_table now_sortable" id="players_totals" data-cols-to-freeze="1"><caption>Totals Table</caption>

然后我从那里引用<td>标签来获取数据;但是,这仅适用于每场比赛,而不是球员总数。我在这里想念什么?

标签: javascriptnode.jsweb-scraping

解决方案


推荐阅读