Creating a single HTML page for a web scraping tool involves integrating HTML, CSS, and JavaScript to provide the described functionality. Below is an example implementation based on your project specification:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Scraping Tool</title>
<style>
/* Basic styling and responsive structure */
body {
font-family: Arial, sans-serif;
margin: 0;
background-color: #e0e0e0;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 20px;
}
.container {
background: #ffffff;
box-shadow: 5px 5px 15px #bebebe, -5px -5px 15px #ffffff;
border-radius: 10px;
padding: 20px;
max-width: 800px;
width: 100%;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
table, th, td {
border: 1px solid #bebebe;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
button {
background-color: #bebebe;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #a0a0a0;
}
.action-buttons {
display: flex;
justify-content: space-between;
}
.input-row input {
width: 100%;
box-sizing: border-box;
}
input[type="checkbox"] {
margin-right: 8px;
}
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
justify-content: center;
align-items: center;
display: none;
}
.overlay-content {
background: #ffffff;
padding: 10px;
border-radius: 8px;
position: relative;
max-width: 500px;
width: 90%;
}
.overlay-content h2 {
margin-top: 0;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Web Scraping Tool</h1>
<table id="data-table">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Selector/Regex</th>
<th>Use Regex</th>
<th>Results</th>
</tr>
</thead>
<tbody id="table-body">
<tr class="input-row">
<td><input type="text" id="name-input"></td>
<td><input type="text" id="url-input"></td>
<td><input type="text" id="selector-input"></td>
<td><input type="checkbox" id="use-regex-input"></td>
<td><button onclick="addRecord()">Add</button></td>
</tr>
</tbody>
</table>
<div class="action-buttons">
<button onclick="exportData()">Export Data</button>
<button onclick="importData()">Import Data</button>
<button onclick="clearRecords()">Clear Records</button>
<button onclick="toggleAbout()">About</button>
</div>
</div>
<div class="overlay" id="about-overlay">
<div class="overlay-content">
<span class="close-btn" onclick="toggleAbout()">X</span>
<h2>About</h2>
<p>This is a Web Scraping Tool capable of extracting data using CSS selectors or regular expressions. Fetch data in real-time and manage your records easily.</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
const tableBody = document.getElementById('table-body');
document.addEventListener('DOMContentLoaded', function() {
loadRecords();
});
function addRecord() {
const nameInput = document.getElementById('name-input').value;
const urlInput = document.getElementById('url-input').value;
const selectorInput = document.getElementById('selector-input').value;
const useRegexInput = document.getElementById('use-regex-input').checked;
if (!nameInput || !urlInput || !selectorInput) {
alert('Please fill in all fields.');
return;
}
const newRow = document.createElement('tr');
newRow.innerHTML = `<td>${nameInput}</td>
<td>${urlInput}</td>
<td>${selectorInput}</td>
<td>${useRegexInput}</td>
<td>
<button onclick="executeRecord('${urlInput}', '${selectorInput}', ${useRegexInput})">Execute</button>
<button onclick="deleteRecord(this)">Delete</button>
</td>`;
tableBody.appendChild(newRow);
saveRecords();
clearInputs();
}
function clearInputs() {
document.getElementById('name-input').value = '';
document.getElementById('url-input').value = '';
document.getElementById('selector-input').value = '';
document.getElementById('use-regex-input').checked = false;
}
function deleteRecord(button) {
const row = button.parentNode.parentNode;
tableBody.removeChild(row);
saveRecords();
}
function executeRecord(url, selector, useRegex) {
axios.get(`https://cors-anywhere.herokuapp.com/${url}`)
.then(response => {
let result;
if (useRegex) {
const regex = new RegExp(selector);
result = response.data.match(regex);
} else {
const parser = new DOMParser();
const doc = parser.parseFromString(response.data, 'text/html');
result = doc.querySelector(selector);
result = result ? result.innerText : 'No data found';
}
alert(`Result: ${result}`);
})
.catch(error => {
console.error('Error fetching data:', error);
alert(`Error: ${error.message}`);
});
}
function saveRecords() {
const rows = tableBody.getElementsByTagName('tr');
const data = Array.from(rows).slice(1).map(row => {
const cells = row.getElementsByTagName('td');
return {
name: cells[0].textContent,
url: cells[1].textContent,
selector: cells[2].textContent,
useRegex: cells[3].textContent === 'true'
};
});
localStorage.setItem('scrapingRecords', JSON.stringify(data));
}
function loadRecords() {
const data = JSON.parse(localStorage.getItem('scrapingRecords') || '[]');
data.forEach(record => {
const newRow = document.createElement('tr');
newRow.innerHTML = `<td>${record.name}</td>
<td>${record.url}</td>
<td>${record.selector}</td>
<td>${record.useRegex}</td>
<td>
<button onclick="executeRecord('${record.url}', '${record.selector}', ${record.useRegex})">Execute</button>
<button onclick="deleteRecord(this)">Delete</button>
</td>`;
tableBody.appendChild(newRow);
});
}
function exportData() {
const data = localStorage.getItem('scrapingRecords');
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'scraping_records.json';
a.click();
URL.revokeObjectURL(url);
}
function importData() {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/json';
input.onchange = event => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const data = JSON.parse(e.target.result);
localStorage.setItem('scrapingRecords', JSON.stringify(data));
location.reload();
};
reader.readAsText(file);
}
};
input.click();
}
function clearRecords() {
localStorage.removeItem('scrapingRecords');
location.reload();
}
function toggleAbout() {
const overlay = document.getElementById('about-overlay');
if (overlay.style.display === 'flex') {
overlay.style.display = 'none';
} else {
overlay.style.display = 'flex';
}
}
</script>
</body>
</html>
This code comprises an HTML page with embedded CSS for styling and JavaScript for functionality. Here’s a breakdown of each component:
- HTML: Structures the webpage with a container for the table and action buttons. It includes an input row that allows users to add scraping records.
- CSS: Provides a basic neumorphic design and responsiveness using Flexbox. It defines specific styles for the table, buttons, and overlays.
- JavaScript: Handles logic for adding, deleting, and executing web scraping records. It uses localStorage to save and load data and employs Axios for HTTP requests.
This solution should fulfill the requirements specified, creating an effective, user-friendly web scraping tool.