Skip to content

Instantly share code, notes, and snippets.

@dragoscv
Created August 24, 2023 21:23
Show Gist options
  • Save dragoscv/9e6170a8a4f86aa5712d04528beaf966 to your computer and use it in GitHub Desktop.
Save dragoscv/9e6170a8a4f86aa5712d04528beaf966 to your computer and use it in GitHub Desktop.
Crawler JavaScript
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