Next.js Discord

Discord Forum

Trouble fetching letters with accent marks

Answered
Bee posted this in #help-forum
Open in Discord
BeeOP
I tried to fetch the html code from a website which has a bunch of names that I want however when I fetch the code letters like à or í or ã appear as � how can I modify the code in order to get either à or just a
  const res = await fetch(`https://randomsite.com`, { cache: "no-store"});
Answered by Bee
 

const buffer = await 
res.arrayBuffer();
const decoder = new TextDecoder('iso-8859-1');
 const htmlBody = decoder.decode(buffer);
View full answer

8 Replies

and make sure your head has a charset such as:
<meta charset="utf-8" />
BeeOP
well the context of this is basically I have a form and I ask for an id in the form which corresponds to a given person in a college for instance 2345 might be John Smith but at that point I don't know the name of the person so I kinda just fetch collegexyz/id?{thisId}.com and then I use cheerio to tap into the html and get the title element which has the name after that I just store the name in my db along with the rest of the info I got from the form but it turns out that the name I get has � in all chars with accents. I looked at the source code for the college site and it has <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-15"> and I have no idea what to do tbh.
          
          const htmlBody = await res.text();
        
          
  
          // Load the HTML content into cheerio
          const $ = cheerio.load(htmlBody);
          
          // Use cheerio selectors to find and extract the name
          const nameElement = $('title');
          
         
          const name = nameElement.text().trim();
BeeOP
// actually never mind I found an old answer to this on stackoverflow and changed it a bit ty
I wanna know the solution you've found 👀
BeeOP
 

const buffer = await 
res.arrayBuffer();
const decoder = new TextDecoder('iso-8859-1');
 const htmlBody = decoder.decode(buffer);
Answer
ahh you needed to decode it first
mark that message as solution