英文:
How to sort through a block of string text
问题
Trying to use data from a basketball stats site for a project. I just need the data to be usable for manipulation (names in one box, correlating stats in the other) as it is right now it's just a long list of strings.
英文:
Trying to use data from a basketball stats site for a project. I just the need the data to be useable for manipulation (names in one box, correlating stats in the other) as it is right now its just a long list of strings.
const axios = require('axios');
const cheerio = require('cheerio')
const fs = require('fs')
const url = 'https://www.basketball-reference.com/leagues/NBA_2023_per_game.html';
axios(url)
.then(response => {
const html = response.data;
const $ = cheerio.load(html)
const fullTable = $('.full_table').text()
console.log(typeof fullTable);
})
.catch(console.error);
Tried a couple of different things (i have 2 other versions of this) and either no data is moved/printed out or it simply gives me all of the data as one big block of strings.
答案1
得分: 1
这是设计如此,当调用 text()
时,你会获得一个长字符串。
const fullTable = $('.full_table').text()
text
获取匹配元素集合中每个元素的组合文本内容,包括其后代元素。
来自文档
虽然 text
很有用,但这不是你所需要的。要获取统计信息,你需要处理每个 tr.full_table
元素,并将其内容提取到一个对象数组中,这样你可以轻松查询。为了高效执行此操作,请参考 extract
方法,了解更多信息和代码示例。
extract
Cheerio 中的 extract 方法允许你从 HTML 文档中提取数据并将其存储在对象中。该方法以一个映射对象作为参数,其中键是要在对象上创建的属性的名称,而值是用于提取值的选择器或描述符。
英文:
It's by design that you're getting a long string when calling text()
const fullTable = $('.full_table').text()
text
> Get the combined text contents of each element in the set of matched elements, including their descendants.
From the docs
While text
is useful, it's not what you need. To get the stats, you'll process each tr.full_table
element and extract the contents to an object array, which you can easily query. To do this efficiently see the extract
method for more information and code examples.
extract
> The extract method in Cheerio allows you to extract data from an HTML document and store it in an object. The method takes a map object as a parameter, where the keys are the names of the properties to be created on the object, and the values are the selectors or descriptors to be used to extract the values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论