Tự động hóa trình duyệt Puppeteer
Các bài liên quan
Puppeteer là một thư viện dành cho Node.js mang đến cơ hội tự động hóa các quy trình bằng trình duyệt dựa trên Chromium thông qua API cấp cao trên Chrome DevTools Protocol. Ví dụ: bạn có thể tạo web crawler tìm kiếm và thu thập dữ liệu bằng cách sử dụng trình duyệt Mimic có dấu vân tay được che.
Phân bổ cổng Multilogin
Trong Multilogin, bạn cần xác định trước cổng ứng dụng để sử dụng tự động hóa Puppeteer.
- Chuyển đến thư mục C:\Users\%username%\.multiloginapp.com và mở tệp app.properties bằng bất kỳ ứng dụng chỉnh sửa text nào.
- Thêm chuỗi sau vào tệp:
multiloginapp.port=[PORT_NUMBER]
- Lưu tệp app.properties
Sau đó, bạn sẽ có thể vào ứng dụng Multilogin thông qua cổng này.
Làm thế nào để bắt đầu
Bước 1
Đảm bảo rằng bạn đã cài đặt Node.js và npm package manager trên PC của mình. Có thể tải xuống Node.js và npm từ trang web chính thức của Node.js (các phiên bản mới nhất của Node.js bao gồm npm theo mặc định). Ngoài ra, bạn có thể sử dụng yarn để quản lý các gói Node.js.
Bạn có thể kiểm tra phiên bản Node.js và npm của mình bằng cách thực hiện các lệnh sau trong terminal:
nodejs -v || node -v && npm -v
Bước 2
Tạo một dự án npm mới trong thư mục hiện tại:
npm init -y
Bước 3
Cài đặt Puppeteer-core trong thư mục dự án:
npm install puppeteer-core@<insert version> --save
Bước 4
Tạo tệp .js bằng code tự động hóa của bạn. Vui lòng sử dụng ví dụ code sau đây để tham khảo:
const puppeteer = require('puppeteer-core');
const http = require('http');
async function startProfile(){
//Replace profileId value with existing browser profile ID.
let profileId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
let mlaPort = 35000;
/*Send GET request to start the browser profile by profileId.
Returns web socket as response which should be passed to puppeteer.connect*/
http.get(`http://127.0.0.1:${mlaPort}/api/v1/profile/start?automation=true&puppeteer=true&profileId=${profileId}`, (resp) => {
let data = '';
let ws = '';
//Receive response data by chunks
resp.on('data', (chunk) => {
data += chunk;
});
/*The whole response data has been received. Handling JSON Parse errors,
verifying if ws is an object and contains the 'value' parameter.*/
resp.on('end', () => {
let ws;
try {
ws = JSON.parse(data);
} catch(err) {
console.log(err);
}
if (typeof ws === 'object' && ws.hasOwnProperty('value')) {
console.log(`Browser websocket endpoint: ${ws.value}`);
run(ws.value);
}
});
}).on("error", (err) => {
console.log(err.message);
});
}
async function run(ws) {
try{
//Connecting Puppeteer with Mimic instance and performing simple automation.
const browser = await puppeteer.connect({browserWSEndpoint: ws, defaultViewport:null});
const page = await browser.newPage();
await page.goto('https://multilogin.com');
await page.screenshot({ path: `/home/${process.env.USER}/Desktop/multiloginScreenshot.png` });
await browser.close();
} catch(err){
console.log(err.message);
}
}
startProfile();
Bước 5
Khởi chạy tệp .js thông qua terminal để bắt đầu tập lệnh tự động hóa của bạn:
nodejs example.js