- vừa được xem lúc

what function are included in CdpHTTPResponse

0 0 7

Người đăng: Tô Văn Thế

Theo Viblo Asia

The HTTPResponse class provides various functions to access information about the HTTP response. Here are some key functions included in the HTTPResponse class:

HTTPResponse Class Functions url(): Returns the URL of the response. status(): Returns the status code of the response (e.g., 200, 404). statusText(): Returns the status text corresponding to the status code (e.g., OK, Not Found). ok(): Returns true if the status code is in the range 200-299. headers(): Returns an object containing all response headers. securityDetails(): Returns an object with security details (e.g., SSL certificate information) if available. buffer(): Returns a Promise that resolves to the response body as a Buffer. text(): Returns a Promise that resolves to the response body as a string. json(): Returns a Promise that resolves to the response body parsed as JSON. request(): Returns the HTTPRequest object that was sent to get this response. fromCache(): Returns true if the response was served from the browser's cache. fromServiceWorker(): Returns true if the response was served by a service worker. Example Usage Here's an example demonstrating how to capture and log HTTP responses using Puppeteer:

const puppeteer = require('puppeteer');
(async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); // Intercept and log HTTP responses page.on('response', async (response) => { const url = response.url(); const status = response.status(); const statusText = response.statusText(); const headers = response.headers(); const responseBody = await response.text(); console.log(`Response URL: ${url}`); console.log(`Status: ${status} ${statusText}`); console.log(`Headers:`, headers); console.log(`Response Body:`, responseBody); }); // Navigate to your target page await page.goto('https://example.com', { waitUntil: 'networkidle2' }); // Continue with your Puppeteer interactions // Example: Clicking a button // await page.click('button-selector'); // Example waiting for some time to capture HTTP responses await page.waitForTimeout(10000); // Adjust time as needed // Close browser when done await browser.close();
})();

Explanation: Interception of Responses: The page.on('response', ...) event listener intercepts all HTTP responses. Logging Details: For each response, the script logs the URL, status, status text, headers, and response body. Utility Functions: The script uses the url(), status(), statusText(), headers(), and text() functions of the HTTPResponse class to retrieve and log these details. Note: Make sure to handle large response bodies carefully to avoid excessive memory usage. Use appropriate error handling and logging for production use. By using the functions provided by the HTTPResponse class, you can effectively capture and process HTTP responses in Puppeteer.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Crawl website sử dụng Node.js và Puppeteer - phần 2

trong phần 1 mình đã giới thiệu về puppeteer và tạo được 1 project cùng một số file đầu tiên để các bạn có thể crawl dữ liệu từ một trang web bất kỳ. Bài này mình sẽ tiếp nối bài viết trước để hoàn thiện seri này.

0 0 68

- vừa được xem lúc

Puppeteer - Giải pháp trích xuất thông tin Web trong NodeJS [Phần 1]

Chào các bạn! Chắc hẳn trong quá trình phát triển một tính năng phía Backend, bạn sẽ gặp những trường hợp cần phải truy cập một trang web và trích xuất thông tin từ chúng vì một mục đích nào đó. Nó gọ

0 0 76

- vừa được xem lúc

Puppeteer - Giải pháp trích xuất thông tin Web trong NodeJS [Phần 2]

Chào các bạn ! Tại bài viết trước , mình đã chia sẽ những kiến thức ban đầu về Headless Browsers và Puppeteer cùng một ví dụ về việc khởi tạo Puppeteer cũng như thực hiện chức năng chụp ảnh từ một tra

0 0 59

- vừa được xem lúc

Hướng dẫn cào dữ liệu từ nguồn trên web với Puppeteer

Hôm trước có dịp đi xe bus trong lúc chờ tàu điện Nhổn ga Hà Nội, trên xe mình gặp một ông cụ khoảng 70 tuổi tóc bạc trắng đang cúi gằm mặt vào 1 quyển sổ toàn những con số 2 chữ số rồi các ô đánh dấu

0 0 24

- vừa được xem lúc

Dockerize project crawl dữ liệu với Puppeteer

Xin chào các bạn, mình là Việt. Sau một thời gian sử dụng Viblo như một công cụ hỗ trợ đọc và tìm hiểu các Tech mới từ các anh chị, các bạn và cũng có cả các em nữa thì hôm nay đây sẽ là bài viết đầu

0 0 12