-
-
Save lujjjh/b8d30f2be4ae82b858bdc22f4c2767df to your computer and use it in GitHub Desktop.
Crawl v2ex.com/my/topics
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
{ | |
const allTopics = []; | |
for (let p = 1; ; p++) { | |
console.log(`fetching page ${p}...`); | |
const response = await fetch(`/my/topics?p=${p}`); | |
const doc = document.createElement("div"); | |
doc.innerHTML = await response.text(); | |
const numOfTopics = +doc.querySelector('a[href="/my/topics"] > .bigger').textContent; | |
const topics = [...doc.querySelectorAll("a.topic-link")].map(({ href, textContent: title }) => ({ href, title })); | |
allTopics.push(...topics); | |
if (!numOfTopics || !topics.length || allTopics.length >= numOfTopics) break; | |
await new Promise(resolve => void setTimeout(resolve, 1000)); | |
} | |
console.table(allTopics); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment