Personal project · 2025
Wikipedia Scraper
A crawler mostly waits, so wait on a hundred pages at once.
A crawler that collects as many unique Wikipedia links as it can in 20 seconds, with 100 workers sharing one queue.
- Stack
- Python, asyncio, aiohttp, BeautifulSoup
Why it matters
Most of a crawler's time goes on waiting: a request goes out, and nothing happens until the server answers. Fetch one page at a time and the program sits idle between answers. Python's asyncio lets a single thread keep many requests open and deal with whichever answers first. This crawler gets 20 seconds to collect as many unique Wikipedia links as it can, starting from the language portal at www.wikipedia.org.
How it works
Start page
The Wikipedia language portal
www.wikipedia.org
Shared queue
Links waiting for a worker
asyncio.Queue · no size limit
100 workers
One thread, one event loop
asyncio tasks
- take the next URL
- skip it if seen
- mark it seen
Wikipedia
Sends back the page
aiohttp · 3 s timeout
200 · text/html
Link filter
Unseen Wikipedia links go back on the queue
BeautifulSoup · html.parser
- "//" links get https:
- other sites dropped
Seen set
Every URL a worker has taken
one Python set, shared
Stop at 20 s
Each worker checks the clock and returns
unique URLs · 10 samples
- deadline passed
- count printed
Decisions I made
A hundred workers on one thread
I used asyncio tasks, not threads: 100 of them share one event loop, one queue and one set of seen URLs, and while one waits on Wikipedia the others carry on. A worker checks the set and adds to it with nothing in between that can pause, so two workers never take the same URL and no lock is needed.
Trade-off. One thread also does all the parsing: while BeautifulSoup reads one page, every other worker waits for it.
Marked as seen when taken, not when found
A URL goes into the seen set the moment a worker takes it off the queue, before the request goes out. Each URL is fetched once, even when the request fails.
Trade-off. A link found on many pages before any worker reaches it goes on the queue once per page, so the queue fills with repeats to skip.
A deadline every worker checks
Nothing cancels the workers. Each one checks the clock before it takes a URL, before it sends a request and between the links it queues, and an idle worker looks again every tenth of a second. A 3-second timeout stops one slow page from holding a worker for long.
Trade-off. A request already in flight at 20 seconds is allowed to finish, so a run can end up to about three seconds late.
The full engineering is on GitHub.