Created
August 24, 2023 21:23
-
-
Save dragoscv/9e6170a8a4f86aa5712d04528beaf966 to your computer and use it in GitHub Desktop.
Crawler JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('https://www.libertatea.ro/') | |
.then(response => response.text()) | |
.then(data => { | |
var parser = new DOMParser(); | |
var doc = parser.parseFromString(data, 'text/html'); | |
// console.log(doc); | |
var articles = []; | |
var articleElements = doc.querySelector('.opinions-and-news ul').querySelectorAll('li'); | |
console.log(articleElements) | |
for (var i = 1; i < Math.min(articleElements.length, 4); i++) { | |
var title = articleElements[i].querySelector('h3.article-title').textContent; | |
var link = articleElements[i].querySelector('a').getAttribute('href'); | |
articles.push({ title: title, link: link }); | |
} | |
console.log(JSON.stringify(articles, null, 2)); | |
}) | |
.catch(error => console. Error(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment