To extract data from the specified URL and export it as JSON using JavaScript, you can use Node.js with the following tools:
axios: For making HTTP requests.
cheerio: For parsing HTML and extracting data.
fs: For writing the JSON output to a file.
Below is a complete example of a Node.js script that fetches the page, parses it, and saves the extracted data as a JSON file. Note that this assumes the data is static and accessible via a simple HTTP GET request.
✅ Step 1: Install Dependencies
Run the following command in your terminal:
bash
npm install axios cheerio
✅ Step 2: Create the JavaScript Script
Save the following code in a file, e.g., extractData.js:
javascript
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
// URL of the target page
const url = 'https://orka.sejm.gov.pl/ArchAll2.nsf/0da1d5d0f70c647bc1258ab300498a96/63109a134d3503aac1258ab3004829b1?OpenDocument';
// Fetch the HTML content
axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
})
.then(async (response) => {
const $ = cheerio.load(response.data);
// Example: Extract all table rows from a table with class "data-table"
const data = [];
$('table.data-table tr').each((index, element) => {
const row = $(element).find('td').map((i, td) => $(td).text().trim()).get();
if (row.length > 0) {
data.push(row);
}
});
// Save the extracted data to a JSON file
fs.writeFileSync('output.json', JSON.stringify(data, null, 2));
console.log('Data successfully saved to output.json');
})
.catch((error) => {
console.error('Error fetching or parsing the page:', error.message);
});
📝 Notes and Adjustments
HTML Structure:
The example assumes the data is in a <table> with class data-table. You may need to inspect the actual HTML of the page and update the selector ('table.data-table tr') accordingly.
Use browser developer tools (F12) to inspect the page and find the correct elements.
Dynamic Content:
If the data is loaded dynamically via JavaScript (e.g., via AJAX), the above approach will not work. In that case, you need a headless browser like Puppeteer.
CORS or Security Restrictions:
The site may block requests from non-browser clients. If you encounter 403 or 401 errors, you may need to add additional headers or cookies, or use a proxy.
User-Agent:
The User-Agent header is added to mimic a browser request. Some sites block requests from automated clients without it.
Always respect the website's robots.txt and terms of service.
If the site uses anti-scraping measures, consider using a proxy or headless browser.
If you're unsure about the HTML structure, post a screenshot or HTML snippet of the page for more accurate guidance.
Let me know if you need help modifying the script for a specific HTML structure!
User
write same program but for all local files stored in "scraped_pages" directory and its subdirectories
Assistant
Here's a Node.js script that processes all HTML files stored in the scraped_pages directory and its subdirectories, extracts structured data using Cheerio, and exports the results as a JSON file.
✅ Features
Traverses a directory and all subdirectories.
Reads all .html files.
Parses HTML content using Cheerio.
Extracts data from a table with class data-table.
Stores the result in a JSON file, including the filename for context.