如何对一段字符串文本进行排序

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 00:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定