Can I Export n8n Scraped Data to CSV Files?
Yes, you can absolutely export scraped data from n8n workflows to CSV files. n8n provides multiple built-in methods to convert your scraped data into CSV format and save it to various destinations. This capability makes it easy to create automated web scraping pipelines that output clean, structured data ready for analysis in spreadsheet applications, databases, or business intelligence tools.
Understanding n8n's CSV Export Capabilities
n8n offers several approaches to export scraped data to CSV format:
- Spreadsheet File Node - Direct CSV file creation and storage
- Code Node - Custom CSV formatting with JavaScript or Python
- Google Sheets Integration - Export to spreadsheet format
- HTTP Request Node - Send CSV data to external services
- Local File System - Write CSV files to disk (self-hosted instances)
Method 1: Using the Spreadsheet File Node (Recommended)
The simplest way to export scraped data to CSV is using n8n's built-in Spreadsheet File node. This node converts JSON data from your scraping workflow into CSV format automatically.
Basic CSV Export Workflow
Here's how to set up a basic scraping-to-CSV workflow:
- Scrape Data - Use HTTP Request or HTML Extract nodes
- Transform Data - Clean and structure your data
- Convert to CSV - Use Spreadsheet File node
- Save or Send - Store the CSV file
Example Configuration
// Sample scraped data structure
[
{
"title": "Product A",
"price": 29.99,
"url": "https://example.com/product-a",
"rating": 4.5
},
{
"title": "Product B",
"price": 39.99,
"url": "https://example.com/product-b",
"rating": 4.2
}
]
Spreadsheet File Node Settings: - Operation: Create - File Format: CSV - Options: - Include headers: ✓ - Delimiter: , (comma) - Quote character: " (double quote)
The node automatically converts your JSON data to CSV format:
title,price,url,rating
Product A,29.99,https://example.com/product-a,4.5
Product B,39.99,https://example.com/product-b,4.2
Method 2: Custom CSV Generation with Code Node
For more control over CSV formatting, use the Code node with JavaScript to create custom CSV files. This approach is useful when you need specific formatting, custom headers, or data transformations.
JavaScript CSV Export Example
// n8n Code Node - JavaScript
const items = $input.all();
const data = [];
// Extract and transform data
for (const item of items) {
data.push({
title: item.json.title,
price: item.json.price,
url: item.json.url,
rating: item.json.rating,
scraped_at: new Date().toISOString()
});
}
// Convert to CSV
function convertToCSV(data) {
if (data.length === 0) return '';
// Get headers
const headers = Object.keys(data[0]);
// Create CSV rows
const csvRows = [headers.join(',')];
for (const row of data) {
const values = headers.map(header => {
const value = row[header];
// Escape quotes and handle special characters
const escaped = ('' + value).replace(/"/g, '""');
return `"${escaped}"`;
});
csvRows.push(values.join(','));
}
return csvRows.join('\n');
}
const csvContent = convertToCSV(data);
return [
{
json: {},
binary: {
data: {
data: Buffer.from(csvContent).toString('base64'),
mimeType: 'text/csv',
fileName: `scraped_data_${Date.now()}.csv`
}
}
}
];
Python CSV Export Example
# n8n Code Node - Python
import csv
from io import StringIO
import base64
from datetime import datetime
items = _input.all()
# Extract data
data = []
for item in items:
data.append({
'title': item['json']['title'],
'price': item['json']['price'],
'url': item['json']['url'],
'rating': item['json']['rating'],
'scraped_at': datetime.now().isoformat()
})
# Create CSV
output = StringIO()
if data:
writer = csv.DictWriter(output, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
csv_content = output.getvalue()
csv_base64 = base64.b64encode(csv_content.encode()).decode()
return [
{
'json': {},
'binary': {
'data': {
'data': csv_base64,
'mimeType': 'text/csv',
'fileName': f'scraped_data_{int(datetime.now().timestamp())}.csv'
}
}
}
]
Method 3: Saving CSV Files to Storage
Once you've converted your scraped data to CSV, you need to save it somewhere. n8n supports multiple storage options:
Save to Google Drive
- Add Google Drive node after Spreadsheet File node
- Set Operation to "Upload"
- Configure:
- File Name:
scraped_data_{{ $now.format('yyyy-MM-dd') }}.csv
- Parents: Select destination folder
- Binary Data: ✓
- File Name:
Save to Dropbox
- Add Dropbox node
- Set Operation to "Upload"
- Configure path and file name
- Enable binary data input
Save to Amazon S3
- Add AWS S3 node
- Set Operation to "Upload"
- Configure:
- Bucket name
- File name
- Binary data input
Save to Local File System (Self-Hosted)
// Code Node - Write to local file system
const fs = require('fs');
const path = require('path');
const items = $input.all();
const csvData = items[0].binary.data.data;
const csvContent = Buffer.from(csvData, 'base64').toString('utf8');
const filePath = path.join('/data/exports', `scraped_${Date.now()}.csv`);
fs.writeFileSync(filePath, csvContent);
return [{ json: { success: true, filePath } }];
Method 4: Direct Export to Google Sheets
While not technically CSV, exporting to Google Sheets provides similar functionality with additional benefits like real-time collaboration and automatic backups. This approach is covered in detail in how to save scraped data to Google Sheets with n8n.
Quick Google Sheets Setup
- Add Google Sheets node
- Set Operation to "Append"
- Configure:
- Document: Select or create spreadsheet
- Sheet: Specify sheet name
- Data Mode: Auto-map columns
Advanced CSV Export Techniques
Handling Large Datasets
When scraping large amounts of data, consider these optimization strategies:
// Batch processing for large datasets
const BATCH_SIZE = 1000;
const items = $input.all();
for (let i = 0; i < items.length; i += BATCH_SIZE) {
const batch = items.slice(i, i + BATCH_SIZE);
const csvContent = convertToCSV(batch);
// Save batch to file
const fileName = `scraped_data_batch_${i / BATCH_SIZE + 1}.csv`;
// ... save logic
}
Custom CSV Formatting Options
// Advanced CSV configuration
const csvOptions = {
delimiter: ';', // Use semicolon instead of comma
quote: '"', // Quote character
escape: '\\', // Escape character
lineDelimiter: '\r\n', // Windows-style line endings
header: true, // Include header row
encoding: 'utf-8' // Character encoding
};
function convertToCSVWithOptions(data, options) {
const delimiter = options.delimiter || ',';
const lineDelimiter = options.lineDelimiter || '\n';
// Implementation here
}
Adding Metadata to CSV Exports
// Add metadata header to CSV
function createCSVWithMetadata(data) {
const metadata = [
`# Scraped on: ${new Date().toISOString()}`,
`# Total records: ${data.length}`,
`# Source: n8n workflow`,
`#`
].join('\n');
const csvData = convertToCSV(data);
return metadata + '\n' + csvData;
}
Scheduling Automated CSV Exports
Combine your scraping workflow with n8n's scheduling capabilities to create automated data exports:
Daily Export Schedule
- Add Schedule Trigger node
- Set to run daily at specific time
- Connect to your scraping workflow
- End with CSV export to storage
Webhook-Triggered Exports
- Add Webhook node as trigger
- Build scraping workflow
- Export to CSV
- Return download link or success message
// Webhook response with CSV download
return [
{
json: {
success: true,
message: 'Data scraped and exported',
download_url: 'https://storage.example.com/exports/data.csv',
records_count: data.length,
timestamp: new Date().toISOString()
}
}
];
Error Handling and Validation
Implement proper error handling to ensure reliable CSV exports:
// Error handling in Code Node
try {
const items = $input.all();
// Validate data
if (!items || items.length === 0) {
throw new Error('No data to export');
}
// Validate required fields
for (const item of items) {
if (!item.json.title || !item.json.price) {
throw new Error('Missing required fields');
}
}
// Convert to CSV
const csvContent = convertToCSV(items.map(i => i.json));
// Return success
return [{ json: { success: true, csvContent } }];
} catch (error) {
// Log error and return failure
console.error('CSV export failed:', error);
return [{
json: {
success: false,
error: error.message
}
}];
}
Best Practices for CSV Exports
- Clean Your Data First - Remove duplicates and handle null values before export
- Use Consistent Headers - Maintain the same column structure across exports
- Handle Special Characters - Properly escape commas, quotes, and newlines
- Add Timestamps - Include scrape time for data tracking
- Validate Output - Check CSV format and data integrity
- Compress Large Files - Use ZIP compression for large datasets
- Set Proper Encoding - Use UTF-8 to support international characters
Troubleshooting Common Issues
Empty CSV Files
Problem: CSV file is created but contains no data
Solution: Check that your scraping nodes are returning data in the expected format. Add a Set node to inspect the data structure before CSV conversion.
Malformed CSV Output
Problem: CSV has incorrect formatting or broken columns
Solution: Ensure proper escaping of special characters. Use the Spreadsheet File node for automatic handling, or implement proper escaping in custom code.
Large File Handling
Problem: Workflow times out with large datasets
Solution: Implement batch processing and split large exports into multiple files. Consider using streaming approaches for very large datasets.
Integration with External Tools
CSV exports from n8n can integrate with various tools:
- Data Analysis: Import into Excel, Pandas, or R
- Business Intelligence: Load into Tableau, Power BI, or Looker
- Databases: Bulk import into PostgreSQL, MySQL, or MongoDB
- Email Reports: Attach CSV files to automated email reports
- FTP/SFTP: Upload to remote servers for distribution
Conclusion
Exporting scraped data to CSV files in n8n is straightforward and flexible. Whether you use the built-in Spreadsheet File node for quick exports or implement custom CSV generation with the Code node, n8n provides all the tools needed for professional data extraction pipelines. Combine these CSV export capabilities with proper error handling and workflow automation to build robust, production-ready scraping solutions.