我正在使用 cheerio 从 https://www.nba.com/players/langston/galloway/204038 获取统计信息,但我无法显示 table 数据

I am using cheerio to grab stats information from https://www.nba.com/players/langston/galloway/204038 but I can't the table data to show up

[the information i want to access][1]


  [1]: https://i.stack.imgur.com/4SpCU.png

无论我做什么,我都无法访问 table 统计信息。我怀疑它与多个 tables 有关,但我不确定。 在此处输入代码

var cheerio = require("cheerio");

   var axios = require("axios");



axios
  .get("https://www.nba.com/players/langston/galloway/204038")
  .then(function (response) {
    var $ = cheerio.load(response.data);

    console.log(
      $("player-detail").find("section.nba-player-stats-traditional").find("td:nth-child(3)").text()

    );


  });

从您的 get 请求返回的实际 html 不包含数据或 table。当您的浏览器加载页面时,将执行一个脚本,该脚本使用 api 调用提取数据并在页面上创建大部分元素。

如果您打开 chrome 开发人员工具 (CTRL+SHIFT+J) 并切换到网络选项卡并重新加载页面,您可以看到所有发生的请求。第一个是在您的 axios GET 请求中下载的 html。如果单击它,您会看到 HTML 与检查页面时看到的内容相比非常基本。

如果您单击 'XHR',将显示为获取数据而进行的大部分 API 调用。 '204038_profile.json' 有一个有趣的。如果您单击它,您可以看到我认为您想要的 JSON 格式的信息,这种格式无需解析 html table 就更容易使用。您可以右键单击“204038_profile.json”并复制完整的 url:

https://data.nba.net/prod/v1/2019/players/204038_profile.json

注意:大多数网站不会喜欢您这样使用他们的数据,您可能需要查看他们的政策。它们可能会使访问数据或随时更改 url 变得更加困难。

您可能想查看 this question or this one 如何加载页面和 运行 javascript 来模拟浏览器。

第二个特别有趣,有一个答案说明如何拦截和改变来自 puppeteer

的请求