Node.js
Node.js SDK (with TypeScript support) for the Rocket Scraper API. For more information, visit the GitHub repository.
Requirements
- Node.js version 18.0 or above
Installation
npm install rocketscraper
Usage
To use the SDK, you need to create a new instance of the RocketClient
class and pass your API key as an argument.
Setup
import { createRocketClient } from 'rocketscraper';
const rocketClient = createRocketClient({
apiKey: 'YOUR_API_KEY',
});
Scrape
The scrape
method allows you to scrape data from a website using a schema. The method returns the scraped data in the format specified in the schema.
import { createRocketClient } from 'rocketscraper';
try {
const rocketClient = createRocketClient({
apiKey: 'YOUR_API_KEY'
});
// Define a comprehensive product schema
const schema = {
productDetails: {
name: 'string',
brand: 'string',
currentPrice: 'number',
originalPrice: 'number',
discount: 'number',
availability: 'boolean',
rating: 'number',
reviewCount: 'integer'
},
specifications: [{
name: 'string',
value: 'string'
}],
shipping: {
freeShipping: 'boolean',
estimatedDays: 'integer'
}
};
// Add a detailed task description for better accuracy (optional)
const taskDescription = `
Extract product information with the following guidelines:
1. For prices, use the main displayed price (ignore bulk discounts)
2. Calculate discount percentage from original and current price
3. Include all technical specifications found on the page
4. Extract shipping details from both product and shipping sections
`;
const result = await rocketClient.scrape({
url: 'https://marketplace.example.com/products/wireless-earbuds',
schema,
taskDescription
});
console.log(result);
} catch (error) {
console.error('Error:', error.message);
}
Example Output
{
"productDetails": {
"name": "Premium Wireless Earbuds Pro X",
"brand": "AudioTech",
"currentPrice": 149.99,
"originalPrice": 199.99,
"discount": 25.0,
"availability": true,
"rating": 4.5,
"reviewCount": 328
},
"specifications": [
{
"name": "Battery Life",
"value": "Up to 8 hours (single charge)"
},
{
"name": "Connectivity",
"value": "Bluetooth 5.2"
},
{
"name": "Water Resistance",
"value": "IPX4"
}
],
"shipping": {
"freeShipping": true,
"estimatedDays": 3
}
}
Error Handling
The SDK will throw errors for various error cases. It's recommended to wrap your API calls in try-catch blocks to handle potential errors gracefully.
Common error scenarios:
- Invalid API key
- Invalid URL
- Invalid schema format
License
This project is licensed under the MIT License. See the LICENSE file for more details.