NodeJs:当开始将阿拉伯语放入JSON中时,将其转换为实体



我正在通过抓取网站构建一个API,该网站是阿拉伯语网站并且很旧。

我首先面临一个问题,当我向网站 Html 发出请求时,服务器会以一些奇怪的符号返回所有阿拉伯文本!!

我已经通过使用这段代码片段称为iconv-lite的库解决了这个问题

// decoding the response to support arabic
responseBody = iconv.decode(Buffer.from(responseBody), "win1256");

现在,如果我返回 HTML 效果很好,但是当我开始解析 HTML 以从中提取数据并将其返回为 JSON 格式时,问题又回来了,阿拉伯语显示为 HTML 实体。

响应是这样的:!!(每个字段中的文字只有阿拉伯文一个(

"                           بيانات الطالب الأساسية            ( دور مايو )"

另外,如果我字符串化数组,由于某种原因问题得到解决!现在我需要解决这个问题,因为我需要数据为 JSON 格式

所有代码

import { Injectable } from '@nestjs/common';
import { load } from 'cheerio';
import { get } from 'request-promise';
import * as iconv from 'iconv-lite';

@Injectable()
export class FacultyService {
async getStudentResultsBody(faculty: string = 'Ektsad', seatNumer: string = '240') {
// search by seatnumber
let responseBody = await get(`http://xxxx/xxxx/${faculty}/xxx.asp?x_level=2019-2018&xxx=LIKE&xxx=${seatNumer}`, {
encoding: null,
});
// decoding the response to support arabic
responseBody = iconv.decode(Buffer.from(responseBody), "win1256");
// parsing the html
let $ = load(responseBody);
const stdCode = $('.aspmaker a').attr('href').replace(/[^0-9]/g, "");
// request to student result page
let studentResultsPage = await get(`http://xxx/xxx/${faculty}/xx.asp?xxx=${stdCode}`, { encoding: null });
// decoding the response to support arabic
studentResultsPage = iconv.decode(Buffer.from(studentResultsPage), "win1256");
return studentResultsPage;
}

async sanatizeData() {
let body = await this.getStudentResultsBody();
let $ = load(body);
let studentDetails: any = $("form > div > table:first-child tr td").map(function (this: any) {
return $(this).html()
.trim()
.split('<br>')
.map(html => html.replace(/<[^>]*>?/gm, '').trim().split(':'));
}).get();

return studentDetails; // getting an issue
}
}

不应关注 JSON 序列化输出中的实体。JSON 仅供机器读取。

用户往往看不到 JSON 版本。

相关内容

最新更新