A Python package for crawling dictionaries, for now just Cambridge
Project description
🦄 Dictionary Crawler
A Python package for crawling dictionary websites. Currently supports Cambridge Dictionary.
Installation
pip install dictionary-crawler
Or install from source:
git clone https://github.com/arshinmq/dictionary-crawler.git
cd dictionary-crawler
pip install -e .
Features
- Async HTTP client for non-blocking requests
- Parse Cambridge Dictionary pages
- Extract:
- Headwords and parts of speech
- Pronunciations (IPA + audio URLs)
- Definitions with CEFR levels
- Examples and sources
- Phrases, idioms, and phrasal verbs
- Collocations
- Verb forms
- Structured Pydantic models for type-safe data access
- JSON serialization support
Quick Start
import asyncio
from dictionary_crawler import CambridgeCrawler
async def main():
async with CambridgeCrawler() as crawler:
page = await crawler.crawl("run")
print(f"Search term: {page.search_term}")
for section in page.sections:
for entry in section.entries:
print(f"\n{entry.headword} ({entry.part_of_speech})")
if entry.pronunciations:
for p in entry.pronunciations:
print(f" [{p.region}] {p.pronunciation}")
for sense in entry.senses:
for defn in sense.definitions:
print(f" - {defn.definition}")
if defn.examples:
for ex in defn.examples:
if ex.example:
print(f" e.g. {ex.example}")
asyncio.run(main())
API Reference
CambridgeCrawler
The main crawler class for Cambridge Dictionary.
from dictionary_crawler import CambridgeCrawler
async with CambridgeCrawler() as crawler:
page = await crawler.crawl(word: str) -> CambridgePage
Parameters
client(optional): Customhttpx.AsyncClientinstance
Data Models
CambridgePage
The root model containing all parsed data:
| Field | Type | Description |
|---|---|---|
search_term |
str |
The word that was searched |
sections |
list[CambridgeSection] |
Dictionary sections (British, American, Business) |
examples |
list[CambridgeExample] | None |
Example sentences |
collocations |
list[CambridgeCollocation] | None |
Collocations |
CambridgeSection
| Field | Type | Description |
|---|---|---|
type |
CambridgeSectionType |
British, American, or Business |
entries |
list[CambridgeEntry] |
Dictionary entries |
CambridgeEntry
| Field | Type | Description |
|---|---|---|
headword |
str |
The word/phrase |
part_of_speech |
str |
noun, verb, adjective, etc. |
pronunciations |
list[CambridgePronunciation] | None |
Pronunciations |
senses |
list[CambridgeSense] |
Word senses with definitions |
phrases |
list[CambridgeSectionPhrase] | None |
Related phrases |
idioms |
list[str] | None |
Related idioms |
phrasal_verbs |
list[str] | None |
Related phrasal verbs |
verb_forms |
list[CambridgeVerbForm] | None |
Verb conjugations |
CambridgePronunciation
| Field | Type | Description |
|---|---|---|
pronunciation |
str |
IPA transcription |
audio_urls |
list[str] |
Audio file URLs |
region |
str |
e.g., "UK", "US" |
CambridgeSense
| Field | Type | Description |
|---|---|---|
cefr |
CEFRLevel | None |
CEFR level (A1-C2) |
definitions |
list[CambridgeDefinition] |
Definitions |
CambridgeDefinition
| Field | Type | Description |
|---|---|---|
definition |
str |
The definition text |
examples |
list[CambridgeDefinitionExample] | None |
Example sentences |
image_urls |
list[str] | None |
Illustration URLs |
CEFRLevel
Enum with values: A1, A2, B1, B2, C1, C2
Exceptions
| Exception | Description |
|---|---|
DictionaryException |
Base exception |
DictionaryFetchException |
HTTP request failed |
DictionaryWordNotFoundException |
Word not in dictionary |
DictionaryParseException |
Failed to parse page |
CambridgeException |
Cambridge-specific base |
CambridgeFetchException |
Cambridge fetch error |
CambridgeWordNotFoundException |
Word not in Cambridge |
CambridgeParseException |
Cambridge parse error |
JSON Export
Models can be serialized to JSON:
page = await crawler.crawl("example")
json_data = page.model_dump_json(indent=2, ensure_ascii=False)
print(json_data)
Development
Setup
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
Running Tests
pytest
Project Structure
dictionary-crawler/
├── src/
│ └── dictionary_crawler/
│ ├── __init__.py
│ ├── base.py # BaseCrawler ABC
│ ├── http.py # HTTP client setup
│ ├── utils.py # URL utilities
│ ├── exceptions.py # Custom exceptions
│ ├── crawlers/
│ │ └── cambridge.py # Cambridge parser
│ └── models/
│ ├── common.py # Shared models (CEFRLevel)
│ └── cambridge.py # Cambridge data models
└── tests/
Roadmap (Future)
- Add Oxford Dictionary support
- Support for additional dictionaries (Merriam-Webster, Collins, etc.)
- Multi-language support (Spanish, French, German, etc.)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dictionary_crawler-0.0.1.tar.gz.
File metadata
- Download URL: dictionary_crawler-0.0.1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18a8746c4488cdf28a193ea872712ff96f93bfbb4f087f9b136b525fc087808e
|
|
| MD5 |
94c08c45409b052c6067ab60a4bace6e
|
|
| BLAKE2b-256 |
302a1083ccf4f1c7cfe081bb37249b51b12498451205f6520b1bd29070222473
|
File details
Details for the file dictionary_crawler-0.0.1-py3-none-any.whl.
File metadata
- Download URL: dictionary_crawler-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc7d640f3e8df7ed84207e2f8703e8fe6c127e00d7a30fea2aea22956a20d579
|
|
| MD5 |
0d187320a7475c12cd328938c3b80fc3
|
|
| BLAKE2b-256 |
e367903a4f00661a43dc26b482655c8ad14234154388351dd3534d911bafa6ba
|