Skip to content

Instantly share code, notes, and snippets.

@lujjjh

lujjjh/main.js Secret

Last active September 10, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lujjjh/b8d30f2be4ae82b858bdc22f4c2767df to your computer and use it in GitHub Desktop.
Save lujjjh/b8d30f2be4ae82b858bdc22f4c2767df to your computer and use it in GitHub Desktop.
Crawl v2ex.com/my/topics
{
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