Sử dụng TypeScript
Việc sử dụng TypeScript giúp đơn giản hóa quá trình phát triển bằng cách cung cấp tính năng tự động gợi ý các phương thức và thuộc tính, cũng như kiểm tra kiểu dữ liệu:

Chúng tôi khuyên bạn nên sử dụng trình soạn thảo Visual Studio Code để tạo và chỉnh sửa các công cụ cào dữ liệu
Cài đặt thư viện kiểu của A-Parser:
cd files/
npm install a-parser-types
A-Parser tự động tạo tệp cấu hình TypeScript cơ bản nếu nó chưa tồn tại:
files/tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"module": "commonjs",
"sourceMap": true,
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "../dist/nodejs/node_modules/*"]
}
},
"include": ["**/*.ts"]
}
Tạo tệp công cụ cào dữ liệu:
mkdir files/parsers/Awesome-Parser/
touch files/parsers/Awesome-Parser/Awesome-Parser.ts
Sử dụng mẫu này để bắt đầu phát triển:
files/parsers/Awesome-Parser/Awesome-Parser.ts
import { BaseParser } from 'a-parser-types';
export class JS_Awesome_Parser extends BaseParser {
static defaultConf: typeof BaseParser.defaultConf = {
version: '0.0.1',
results: {
flat: [
['title', 'Title'],
],
arrays: {
}
},
results_format: "Title: $title\n",
};
static editableConf: typeof BaseParser.editableConf = [
];
async parse(set, results) {
...
return results;
}
}
ghi chú
- Generator được thay thế bằng
async/await
Khác biệt so với API v1
- Generator được thay thế bằng
async/await - Các trường
defaultConfvàeditableConftrở thànhstatic - Lớp công cụ cào dữ liệu phải được kế thừa từ
BaseParser - TypeScript có thể được sử dụng tùy chọn, chúng tôi khuyên bạn nên sử dụng nó theo mặc định để làm nổi bật các phương thức và tham số có sẵn