Phiên Làm Việc Trực Tiếp
Tính năng xem trực tiếp của trình duyệt scraping cho phép bạn xem và điều khiển các phiên trình duyệt trong thời gian thực. Cụ thể, tính năng xem trực tiếp bao gồm xem, nhấp chuột, nhập liệu và cuộn trong bất kỳ phiên trình duyệt nào đang hoạt động. Do đó, bạn có thể dễ dàng theo dõi các quy trình tự động, gỡ lỗi các script tự động và can thiệp thủ công vào các phiên trình duyệt.
Trong scrapeless, bạn có thể xem hoặc điều khiển các phiên trình duyệt ở hai nơi: playground và giao diện quản lý phiên.
Cách Sử Dụng
Tạo Phiên Trình Duyệt Scrapeless
Đầu tiên, bạn cần tạo một phiên. Có hai cách để làm điều này:
Tạo Phiên thông qua Playground
Tạo Phiên thông qua API
Bạn cũng có thể sử dụng API của chúng tôi để tạo một phiên. Tham khảo tài liệu API: Tài liệu API Trình Duyệt Scraping. Tính năng phiên của chúng tôi sẽ giúp bạn quản lý phiên này, bao gồm cả chức năng xem trực tiếp.
const { Scrapeless } = require('@scrapeless-ai/sdk');
const puppeteer =require('puppeteer-core');
const client = new Scrapeless({ apiKey: 'API Key' });
// custom fingerprint
const fingerprint = {
platform: 'Windows',
}
// Create browser session and get WebSocket endpoint
const { browserWSEndpoint } = client.browser.create({
session_name: 'sdk_test',
session_ttl: 180,
proxy_country: 'US',
session_recording: true,
fingerprint,
});
(async () => {
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
await page.goto('https://www.scrapeless.com');
await new Promise(res => setTimeout(res, 3000));
await page.goto('https://www.google.com');
await new Promise(res => setTimeout(res, 3000));
await page.goto('https://www.youtube.com');
await new Promise(res => setTimeout(res, 3000));
await browser.close();
})();
Xem Phiên Trực Tiếp
Trong giao diện quản lý phiên scrapeless, bạn có thể dễ dàng xem các phiên trực tiếp. Cũng có hai cách để xem chúng:
Xem Phiên Playground Trong Thời Gian Thực
Sau khi tạo một phiên trong playground, bạn có thể thấy trình duyệt chạy trong thời gian thực ở phía bên phải.
Xem Phiên API Trong Thời Gian Thực
Sau khi tạo một phiên thông qua API, bạn có thể thấy danh sách các phiên đang chạy trên trang phiên. Nhấp vào chi tiết Hành động cho phép bạn xem trước hoạt động của trình duyệt trong thời gian thực. Ở đây bạn có thể chọn xem phiên trực tiếp tại chỗ hoặc sao chép URL phiên để xem phiên trực tiếp. Chúng tôi cung cấp hai video hoạt động để bạn tham khảo.
Hiển Thị Tại Chỗ
Lấy URL Trực Tiếp thông qua website
Bạn có thể sao chép URL Trực tiếp từ danh sách các phiên đang chạy và dán nó vào trình duyệt để truy cập trực tiếp.
Lấy URL Trực Tiếp thông qua API
Bạn có thể lấy URL Trực tiếp bằng cách gọi API. Trong ví dụ mã sau, chúng tôi trước tiên lấy tất cả các phiên đang chạy bằng API Phiên Đang Chạy, sau đó lấy URL Trực tiếp của một phiên cụ thể bằng API URL Trực Tiếp:
const API_CONFIG = {
host: 'https://api.scrapeless.com',
headers: {
'x-api-token': 'API Key',
'Content-Type': 'application/json'
}
};
const requestOptions = {
method: 'GET',
headers: new Headers(API_CONFIG.headers)
};
async function fetchBrowserSessions() {
try {
// Fetch running browser sessions
const sessionResponse = await fetch(`${API_CONFIG.host}/browser/running`, requestOptions);
if (!sessionResponse.ok) {
throw new Error(`failed to fetch sessions: ${sessionResponse.status} ${sessionResponse.statusText}`);
}
const sessionResult = await sessionResponse.json();
// Process sessions data
const sessions = sessionResult.data;
if (!sessions || !Array.isArray(sessions) || sessions.length === 0) {
console.log("no active browser sessions found");
return;
}
// Get first session task ID
const taskId = sessions[0]?.taskId;
if (!taskId) {
console.log("task id not found in the session data");
return;
}
// Fetch live URL for the task
await fetchLiveUrl(taskId);
} catch (error) {
console.error("error fetching browser sessions:", error.message);
}
}
async function fetchLiveUrl(taskId) {
try {
const liveResponse = await fetch(`${API_CONFIG.host}/browser/${taskId}/live`, requestOptions);
if (!liveResponse.ok) {
throw new Error(`failed to fetch live url: ${liveResponse.status} ${liveResponse.statusText}`);
}
const liveResult = await liveResponse.json();
if (liveResult && liveResult.data) {
console.log(`taskId: ${taskId}`);
console.log(`liveUrl: ${liveResult.data}`);
} else {
console.log("no live url data available for this task");
}
} catch (error) {
console.error(`error fetching live url for task ${taskId}:`, error.message);
}
}
fetchBrowserSessions().then(r => { });
Lấy URL Trực Tiếp thông qua CDP
Để lấy URL Trực tiếp trong khi mã của bạn đang chạy, hãy gọi lệnh cdp Agent.liveURL:
const { Puppeteer, log as Log } = require('@scrapeless-ai/sdk');
const logger = Log.withPrefix('puppeteer-example');
(async () => {
const browser = await Puppeteer.connect({
session_name: 'sdk_test',
session_ttl: 180,
proxy_country: 'US',
session_recording: true,
defaultViewport: null
});
const page = await browser.newPage();
await page.goto('https://www.scrapeless.com');
const { error, liveURL } = await page.liveURL();
if (error) {
logger.error('Failed to get current page URL:', error);
} else {
logger.info('Current page URL:', liveURL);
}
await browser.close();
})();