Created
November 14, 2022 15:21
-
-
Save mnmkng/8aec9cd9dbed3f1b197cd82444bbb94a to your computer and use it in GitHub Desktop.
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
import { PlaywrightCrawler } from 'crawlee'; | |
const crawler = new PlaywrightCrawler({ | |
requestHandler: async ({ page }) => { | |
// Extract data from the page. Selecting all 'article' elements | |
// will return all the repository cards we're looking for. | |
const repos = await page.$$eval('article.border', (repoCards) => { | |
return repoCards.map(card => { | |
const [user, repo] = card.querySelectorAll('h3 a'); | |
const stars = card.querySelector('#repo-stars-counter-star').getAttribute('title'); | |
const description = card.querySelector('div.px-3 > p + div'); | |
const topics = card.querySelectorAll('a.topic-tag'); | |
const toText = (element) => element && element.innerText.trim(); | |
const parseNumber = (text) => Number(text.replace(/,/g, '')); | |
return { | |
user: toText(user), | |
repo: toText(repo), | |
url: repo.href, | |
stars: parseNumber(stars), | |
description: toText(description), | |
topics: Array.from(topics).map((t) => toText(t)), | |
}; | |
}); | |
}); | |
// Print the results. Nice! | |
console.log(`We extracted ${repos.length} repositories.`); | |
console.dir(repos); | |
} | |
}) | |
await crawler.run(['https://github.com/topics/javascript']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment