Tổng quan
JavaScript API v2 mới được thiết kế để đơn giản hóa công việc bằng cách loại bỏ việc sử dụng generator và chuyển sang async/await. Ngoài ra, API này đã khắc phục một số hạn chế của phiên bản trước, thêm hỗ trợ TypeScript và tăng hiệu suất. Chúng tôi khuyên bạn nên sử dụng JavaScript API này để tạo tất cả các công cụ cào dữ liệu mới
Để sử dụng JavaScript API v2, chỉ cần kế thừa lớp công cụ cào dữ liệu của bạn từ lớp cơ sở BaseParser. Hãy cùng phân tích cấu trúc của lớp công cụ cào dữ liệu qua ví dụ sau:
- TypeScript
- JavaScript
import { BaseParser } from 'a-parser-types';
export class JS_v2_example extends BaseParser {
static defaultConf: typeof BaseParser.defaultConf = {
version: '0.0.1',
results: {
flat: [
['title', 'Title'],
['h1', 'H1 Header']
],
arrays: {
h2: ['H2 Headers List', [
['header', 'Header'],
]],
}
},
max_size: 2 * 1024 * 1024,
parsecodes: {
200: 1,
},
results_format: "Title: $title\nH1: $h1\nH2 headers:\n$h2.format('$header\\n')\n",
limitH2Tags: 3,
};
static editableConf: typeof BaseParser.editableConf = [
['limitH2Tags', ['textfield', 'Limit H2 tags']],
];
async parse(set, results) {
const { success, data, headers } = await this.request('GET', set.query);
if (success && typeof data == 'string') {
let matches;
if (matches = data.match(/<title[^>]*>(.*?)<\/title>/))
results.title = matches[1];
if (matches = data.match(/<h1[^>]*>(.*?)<\/h1>/))
results.h1 = matches[1];
if (results.h2) {
let count = 0;
const re = /<h2[^>]*>(.*?)<\/h2>/g;
while(matches = re.exec(data)) {
results.h2.push(matches[1]);
if (++count == this.conf.limitH2Tags)
break;
}
}
}
return results;
}
}
const { BaseParser } = require("a-parser-types");
class JS_v2_example_js extends BaseParser {
static defaultConf = {
version: '0.0.1',
results: {
flat: [
['title', 'Title'],
['h1', 'H1 Header']
],
arrays: {
h2: ['H2 Headers List', [
['header', 'Header'],
]],
}
},
max_size: 2 * 1024 * 1024,
parsecodes: {
200: 1,
},
results_format: "Title: $title\nH1: $h1\nH2 headers:\n$h2.format('$header\\n')\n",
limitH2Tags: 3,
};
static editableConf = [
['limitH2Tags', ['textfield', 'Limit H2 tags']],
];
async parse(set, results) {
const { success, data, headers } = await this.request('GET', set.query);
if (success && typeof data == 'string') {
let matches;
if (matches = data.match(/<title[^>]*>(.*?)<\/title>/))
results.title = matches[1];
if (matches = data.match(/<h1[^>]*>(.*?)<\/h1>/))
results.h1 = matches[1];
if (results.h2) {
let count = 0;
const re = /<h2[^>]*>(.*?)<\/h2>/g;
while(matches = re.exec(data)) {
results.h2.push(matches[1]);
if (++count == this.conf.limitH2Tags)
break;
}
}
}
return results;
}
}
TODO: (next) ## Kế thừa
Liên kết hữu ích
🔗 Ví dụ lưu tệp vào đĩa
Ví dụ minh họa cách lưu tệp trực tiếp vào đĩa
🔗 Ví dụ làm việc với phiên (session)
Sử dụng chức năng phiên trong các công cụ cào dữ liệu JavaScript
🔗 Ví dụ lưu dữ liệu trong phiên
Minh họa khả năng lưu trữ dữ liệu tùy ý trong phiên
🔗 Sử dụng results.addElement()
Ví dụ điền mảng dữ liệu bằng results.addElement() và minh họa sự khác biệt so với .push() thông thường