0

I am trying to show rankings in fencing using cheerio and find that the rankings, points and T-P (not sure what T-P is in this case and am going to remove this) are all combined.

Example:

rank: 1 
Points: 68 
T-P: 0 

Result after running code: 1680

This is all different td classes so im thinking that its combining all 3 since they have the "same name"

The code in question (yes it is from a tutorial website i started with this like an hour ago)

async function performScraping() {
  // downloading the target web page
  // by performing an HTTP GET request in Axios
  const axiosResponse = await axios.request({
    method: "GET",
    url: "https://fencing.ophardt.online/en/show-ranking/html/60198",
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
    },
  });
  // console.log (axiosResponse.data)
  const $ = cheerio.load(axiosResponse.data);
  var things = $(".rankingbody >tbody >tr").toArray();

  for (var thing of things) {
    var row = $(thing).find(".ranking:not(:has(div))");
    var name = $(thing).find(".ranking > .btn-group > a");

    console.log(name.text().replace(/\s\s+/g, " "));
    console.log(row.text().replace(/\s\s+/g, " "));
  }
}

performScraping();

I've tried searching for splitting the td's but it doesn't seem to give the results I'm looking for.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • I would avoid using regex 99% of the time when scraping. In this case, try `const rowText = [...$(thing).find(".ranking td")].map(e => $(e).text().trim());`, similar to [this answer](https://stackoverflow.com/a/74973573/6243352) from the dupe thread (`.get()` is pretty much the same as spreading for all intents and purposes). The only regex I'd apply here is if you wanted to parse out parts of that `name`, because there are no more HTML elements to work with. – ggorlen Aug 27 '23 at 19:29
  • you can also try adding space after the td's: `$('td'),after("\n")` – pguardiario Aug 28 '23 at 00:38

0 Answers0