// Function to extract images and text from the web page function scrapeWebStory() { // Create an object to store extracted data const data = { images: [], text: [] }; // Extract all text content document.querySelectorAll('p').forEach((element) => { data.text.push(element.textContent.trim()); }); // Extract all image URLs document.querySelectorAll('img').forEach((element) => { let src = element.getAttribute('src'); if (src) { // Handle relative URLs if (!src.startsWith('http')) { src = new URL(src, window.location.href).href; } data.images.push(src); } }); // Print or save the extracted data console.log('Extracted Data:', data); // Optionally, you can create a downloadable file or further process the data // Example: create a text file with the text content const textBlob = new Blob([data.text.join('\n')], { type: 'text/plain' }); const textUrl = URL.createObjectURL(textBlob); const textLink = document.createElement('a'); textLink.href = textUrl; textLink.download = 'text_content.txt'; textLink.click(); // Example: download images data.images.forEach((imgUrl, index) => { fetch(imgUrl) .then(response => response.blob()) .then(blob => { const imgBlobUrl = URL.createObjectURL(blob); const imgLink = document.createElement('a'); imgLink.href = imgBlobUrl; imgLink.download = `image_${index + 1}.jpg`; imgLink.click(); }); }); } // Run the function to start scraping scrapeWebStory();
0 Comments