# SriLanka Lottery
The `srilanka-lottery` package is a Python library designed to scrape lottery results and related information from Sri Lanka's National Lottery Board (NLB) and Development Lottery Board (DLB) websites. It provides a simple and efficient way to access lottery data, including specific draw results by draw number or date, lists of available lotteries, and the latest results for both NLB and DLB lotteries. Whether you're building a data analysis tool, a lottery result aggregator, or simply exploring lottery data, this package streamlines the process with robust web scraping capabilities.
## Features
- Retrieve specific lottery results by draw number or date for NLB and DLB lotteries.
- Scrape lists of active NLB lotteries and available DLB lotteries.
- Fetch the latest results for a specified number of draws.
- Handle cookies and session management automatically for reliable scraping.
- Well-documented functions with error handling for robust usage.
- Lightweight and easy to integrate into larger projects.
## Installation
### Prerequisites
- Python 3.6 or higher
- `pip` (Python package manager)
### Install via PyPI
To install the `srilanka-lottery` package, run the following command:
```bash
pip install srilanka-lottery
```
This will automatically install the required dependencies: `requests` and `beautifulsoup4`.
### Install from Source
If you prefer to install from the source code (e.g., for development or customization):
1. Clone the repository:
```bash
git clone https://github.com/ishanoshada/Srilanka-Lottery.git
cd Srilanka-Lottery
```
2. Install the dependencies:
```bash
pip install -r requirements.txt
```
3. Install the package locally:
```bash
pip install .
```
## Usage
The `srilanka-lottery` package provides several functions to interact with NLB and DLB lottery data. Below are detailed examples demonstrating how to use each function.
### Example 1: Scrape Lottery Names
Retrieve the list of available lotteries from DLB and active lotteries from NLB.
```python
from srilanka_lottery import scrape_dlb_lottery_names, scrape_nlb_active_lottery_names
# Scrape DLB lottery names
dlb_names = scrape_dlb_lottery_names()
print("DLB Lotteries:", dlb_names.get("DLB", dlb_names.get("error")))
# Scrape NLB active lottery names
nlb_names, session = scrape_nlb_active_lottery_names()
print("NLB Active Lotteries:", nlb_names.get("NLB_Active", nlb_names.get("error")))
```
**Example Output**:
```
DLB Lotteries: ['Ada Kotipathi', 'Jayoda', 'Lagna Wasana', ...]
NLB Active Lotteries: ['Mega Power', 'Dhana Nidhanaya', ...]
```
### Example 2: Scrape Specific Lottery Results
Fetch results for a specific draw number or date for an NLB or DLB lottery.
```python
from srilanka_lottery import scrape_nlb_result, scrape_dlb_result
# Fetch NLB result by draw number
nlb_result = scrape_nlb_result("mega-power", 2166)
print("NLB Result (Draw 2166):", nlb_result)
# Fetch NLB result by date
nlb_result_date = scrape_nlb_result("mega-power", "2025-05-01")
print("NLB Result (2025-05-01):", nlb_result_date)
# Fetch DLB result by draw number
dlb_result = scrape_dlb_result("Ada Kotipathi", 2608)
print("DLB Result (Draw 2608):", dlb_result)
# Fetch DLB result by date
dlb_result_date = scrape_dlb_result("Ada Kotipathi", "2025-05-01")
print("DLB Result (2025-05-01):", dlb_result_date)
```
**Example Output**:
```
NLB Result (Draw 2166): {'draw_number': '2166', 'date': '2025-05-01', 'letter': 'A', 'numbers': ['12', '34', '56', '78']}
NLB Result (2025-05-01): {'draw_number': '2166', 'date': '2025-05-01', 'letter': 'A', 'numbers': ['12', '34', '56', '78']}
DLB Result (Draw 2608): {'draw_info': 'Ada Kotipathi 2608', 'date_info': '2025-05-01', 'letter': 'Y', 'numbers': ['11', '22', '33', '44'], 'prize_image': ''}
DLB Result (2025-05-01): {'draw_info': 'Ada Kotipathi 2608', 'date_info': '2025-05-01', 'letter': 'Y', 'numbers': ['11', '22', '33', '44'], 'prize_image': ''}
```
### Example 3: Scrape Latest Results
Retrieve the latest results for a specified number of draws.
```python
from srilanka_lottery import scrape_nlb_latest_results, scrape_dlb_latest_results, scrape_nlb_active_lottery_names
# Get NLB session
_, session = scrape_nlb_active_lottery_names()
# Fetch latest NLB results
nlb_latest = scrape_nlb_latest_results(session, "mega-power", limit=3)
for result in nlb_latest.get("NLB_Results", []):
print(f"NLB Draw {result['draw']} ({result['date']}): {result['letter']} {result['numbers']}")
# Fetch latest DLB results
dlb_latest = scrape_dlb_latest_results("Ada Kotipathi", limit=3)
for result in dlb_latest.get("DLB_Results", []):
print(f"DLB Draw {result['draw']} ({result['date']}): {result['letter']} {result['numbers']}")
```
**Example Output**:
```
NLB Draw 2166 (2025-05-01): A ['12', '34', '56', '78']
NLB Draw 2165 (2025-04-30): B ['15', '25', '45', '65']
NLB Draw 2164 (2025-04-29): C ['10', '20', '30', '40']
DLB Draw 2608 (2025-05-01): Y ['11', '22', '33', '44']
DLB Draw 2607 (2025-04-30): Z ['12', '23', '34', '45']
DLB Draw 2606 (2025-04-29): X ['13', '24', '35', '46']
```
## Functions
The package includes the following functions:
- **`scrape_nlb_result(lottery_name, draw_or_date)`**: Fetch NLB lottery results by draw number (int) or date (str, YYYY-MM-DD). Returns a dictionary with draw number, date, letter, and numbers.
- **`scrape_dlb_result(lottery_name, draw_or_date)`**: Fetch DLB lottery results by draw number or date. Returns a dictionary with draw info, date, letter, numbers, and prize image URL.
- **`scrape_dlb_lottery_names()`**: Retrieve a list of available DLB lotteries. Returns a dictionary with a sorted list of names or an error message.
- **`scrape_nlb_active_lottery_names()`**: Retrieve a list of active NLB lotteries. Returns a tuple containing a dictionary with a sorted list of names or an error message and the session object.
- **`scrape_nlb_latest_results(session, lottery_name, limit=5)`**: Fetch the latest NLB lottery results up to the specified limit. Requires a session from `scrape_nlb_active_lottery_names`. Returns a dictionary with a list of results.
- **`scrape_dlb_latest_results(lottery_name, limit=5)`**: Fetch the latest DLB lottery results up to the specified limit. Returns a dictionary with a list of results.
## Supported Lotteries
### DLB Lotteries
The package supports the following DLB lotteries (as of the latest update):
- Ada Kotipathi
- Jayoda
- Lagna Wasana
- Sasiri
- Shanida
- Super Ball
- Supiri Dhana Sampatha
- Jaya Sampatha
- Kapruka
### NLB Lotteries
The package can scrape results for any active NLB lottery. Use `scrape_nlb_active_lottery_names()` to get the current list of active lotteries. Examples include:
- Mega Power
- Dhana Nidhanaya
- Mahajana Sampatha
- And more, depending on the active lotteries at the time of scraping.
## Troubleshooting
### Common Issues
- **"Result block not found" or "Request failed" errors**:
- Ensure you have a stable internet connection.
- The NLB or DLB website structure may have changed. Check the website manually and consider updating the package or reporting an issue.
- The draw number or date may not exist. Verify the input parameters.
- **Cookie extraction failing**:
- The `setCookie` function in the website’s JavaScript may have changed. Report this issue to the package maintainers.
- **Dependencies not installed**:
- Run `pip install requests beautifulsoup4` to ensure dependencies are installed.
### Debugging Tips
- Enable verbose logging by modifying the code to print additional debug information (e.g., HTTP response status codes).
- Check the raw HTML response if parsing fails using `print(response.text)` in the relevant function.
- Test with different draw numbers or dates to isolate the issue.
## FAQ
### Q: What do I need to use this package?
A: You need Python 3.6 or higher and the `requests` and `beautifulsoup4` libraries, which are automatically installed when you install the package via pip.
### Q: Can I scrape results for any NLB lottery?
A: Yes, as long as the lottery is active. Use `scrape_nlb_active_lottery_names()` to get the current list of active lotteries.
### Q: Why am I getting an "error" key in the response?
A: The error key indicates a problem, such as an invalid lottery name, unreachable website, or missing data. Check the error message for details and ensure your inputs are correct.
### Q: Is this package legal to use?
A: This package is designed for educational and personal use to scrape publicly available data. Ensure you comply with the terms of service of the NLB and DLB websites and applicable laws in your jurisdiction.
### Q: How do I contribute to this package?
A: Contributions are welcome! See the "Contributing" section below for details.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Contributing
We welcome contributions to improve the `srilanka-lottery` package! To contribute:
1. Fork the repository on GitHub.
2. Create a new branch for your feature or bug fix.
3. Make your changes and write tests if applicable.
4. Submit a pull request with a clear description of your changes.
Please ensure your code follows the existing style and includes appropriate documentation. For major changes, open an issue first to discuss your proposal.
## Contact
For questions, bug reports, or support, please contact [ic31908@gmail.com](mailto:ic31908@gmail.com) or open an issue on the [GitHub repository](https://github.com/ishanoshada/srilanka-lottery).
## Acknowledgements
- Built with [requests](https://requests.readthedocs.io/) and [BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup/).
- Inspired by the need for accessible lottery data in Sri Lanka.
- Thanks to the open-source community for providing tools and resources.
**Repository Views** 
Raw data
{
"_id": null,
"home_page": "https://github.com/ishanoshada/srilanka-lottery",
"name": "srilanka-lottery",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "lottery, Sri Lanka lottery, NLB, DLB, web scraping, lottery results, Python lottery scraper, lottery data, National Lottery Board, Development Lottery Board, lottery API, data extraction",
"author": "Ishan Oshada",
"author_email": "ic31908@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d1/ac/194bcf3600a5f18b32f35883337d13440c2d03f9056f0af6f11a99cd8e28/srilanka_lottery-0.1.2.tar.gz",
"platform": null,
"description": "\n# SriLanka Lottery\n\nThe `srilanka-lottery` package is a Python library designed to scrape lottery results and related information from Sri Lanka's National Lottery Board (NLB) and Development Lottery Board (DLB) websites. It provides a simple and efficient way to access lottery data, including specific draw results by draw number or date, lists of available lotteries, and the latest results for both NLB and DLB lotteries. Whether you're building a data analysis tool, a lottery result aggregator, or simply exploring lottery data, this package streamlines the process with robust web scraping capabilities.\n\n## Features\n\n- Retrieve specific lottery results by draw number or date for NLB and DLB lotteries.\n- Scrape lists of active NLB lotteries and available DLB lotteries.\n- Fetch the latest results for a specified number of draws.\n- Handle cookies and session management automatically for reliable scraping.\n- Well-documented functions with error handling for robust usage.\n- Lightweight and easy to integrate into larger projects.\n\n## Installation\n\n### Prerequisites\n\n- Python 3.6 or higher\n- `pip` (Python package manager)\n\n### Install via PyPI\n\nTo install the `srilanka-lottery` package, run the following command:\n\n```bash\npip install srilanka-lottery\n```\n\nThis will automatically install the required dependencies: `requests` and `beautifulsoup4`.\n\n### Install from Source\n\nIf you prefer to install from the source code (e.g., for development or customization):\n\n1. Clone the repository:\n ```bash\n git clone https://github.com/ishanoshada/Srilanka-Lottery.git\n cd Srilanka-Lottery\n ```\n\n2. Install the dependencies:\n ```bash\n pip install -r requirements.txt\n ```\n\n3. Install the package locally:\n ```bash\n pip install .\n ```\n\n## Usage\n\nThe `srilanka-lottery` package provides several functions to interact with NLB and DLB lottery data. Below are detailed examples demonstrating how to use each function.\n\n### Example 1: Scrape Lottery Names\n\nRetrieve the list of available lotteries from DLB and active lotteries from NLB.\n\n```python\nfrom srilanka_lottery import scrape_dlb_lottery_names, scrape_nlb_active_lottery_names\n\n# Scrape DLB lottery names\ndlb_names = scrape_dlb_lottery_names()\nprint(\"DLB Lotteries:\", dlb_names.get(\"DLB\", dlb_names.get(\"error\")))\n\n# Scrape NLB active lottery names\nnlb_names, session = scrape_nlb_active_lottery_names()\nprint(\"NLB Active Lotteries:\", nlb_names.get(\"NLB_Active\", nlb_names.get(\"error\")))\n```\n\n**Example Output**:\n```\nDLB Lotteries: ['Ada Kotipathi', 'Jayoda', 'Lagna Wasana', ...]\nNLB Active Lotteries: ['Mega Power', 'Dhana Nidhanaya', ...]\n```\n\n### Example 2: Scrape Specific Lottery Results\n\nFetch results for a specific draw number or date for an NLB or DLB lottery.\n\n```python\nfrom srilanka_lottery import scrape_nlb_result, scrape_dlb_result\n\n# Fetch NLB result by draw number\nnlb_result = scrape_nlb_result(\"mega-power\", 2166)\nprint(\"NLB Result (Draw 2166):\", nlb_result)\n\n# Fetch NLB result by date\nnlb_result_date = scrape_nlb_result(\"mega-power\", \"2025-05-01\")\nprint(\"NLB Result (2025-05-01):\", nlb_result_date)\n\n# Fetch DLB result by draw number\ndlb_result = scrape_dlb_result(\"Ada Kotipathi\", 2608)\nprint(\"DLB Result (Draw 2608):\", dlb_result)\n\n# Fetch DLB result by date\ndlb_result_date = scrape_dlb_result(\"Ada Kotipathi\", \"2025-05-01\")\nprint(\"DLB Result (2025-05-01):\", dlb_result_date)\n```\n\n**Example Output**:\n```\nNLB Result (Draw 2166): {'draw_number': '2166', 'date': '2025-05-01', 'letter': 'A', 'numbers': ['12', '34', '56', '78']}\nNLB Result (2025-05-01): {'draw_number': '2166', 'date': '2025-05-01', 'letter': 'A', 'numbers': ['12', '34', '56', '78']}\nDLB Result (Draw 2608): {'draw_info': 'Ada Kotipathi 2608', 'date_info': '2025-05-01', 'letter': 'Y', 'numbers': ['11', '22', '33', '44'], 'prize_image': ''}\nDLB Result (2025-05-01): {'draw_info': 'Ada Kotipathi 2608', 'date_info': '2025-05-01', 'letter': 'Y', 'numbers': ['11', '22', '33', '44'], 'prize_image': ''}\n```\n\n### Example 3: Scrape Latest Results\n\nRetrieve the latest results for a specified number of draws.\n\n```python\nfrom srilanka_lottery import scrape_nlb_latest_results, scrape_dlb_latest_results, scrape_nlb_active_lottery_names\n\n# Get NLB session\n_, session = scrape_nlb_active_lottery_names()\n\n# Fetch latest NLB results\nnlb_latest = scrape_nlb_latest_results(session, \"mega-power\", limit=3)\nfor result in nlb_latest.get(\"NLB_Results\", []):\n print(f\"NLB Draw {result['draw']} ({result['date']}): {result['letter']} {result['numbers']}\")\n\n# Fetch latest DLB results\ndlb_latest = scrape_dlb_latest_results(\"Ada Kotipathi\", limit=3)\nfor result in dlb_latest.get(\"DLB_Results\", []):\n print(f\"DLB Draw {result['draw']} ({result['date']}): {result['letter']} {result['numbers']}\")\n```\n\n**Example Output**:\n```\nNLB Draw 2166 (2025-05-01): A ['12', '34', '56', '78']\nNLB Draw 2165 (2025-04-30): B ['15', '25', '45', '65']\nNLB Draw 2164 (2025-04-29): C ['10', '20', '30', '40']\nDLB Draw 2608 (2025-05-01): Y ['11', '22', '33', '44']\nDLB Draw 2607 (2025-04-30): Z ['12', '23', '34', '45']\nDLB Draw 2606 (2025-04-29): X ['13', '24', '35', '46']\n```\n\n## Functions\n\nThe package includes the following functions:\n\n- **`scrape_nlb_result(lottery_name, draw_or_date)`**: Fetch NLB lottery results by draw number (int) or date (str, YYYY-MM-DD). Returns a dictionary with draw number, date, letter, and numbers.\n- **`scrape_dlb_result(lottery_name, draw_or_date)`**: Fetch DLB lottery results by draw number or date. Returns a dictionary with draw info, date, letter, numbers, and prize image URL.\n- **`scrape_dlb_lottery_names()`**: Retrieve a list of available DLB lotteries. Returns a dictionary with a sorted list of names or an error message.\n- **`scrape_nlb_active_lottery_names()`**: Retrieve a list of active NLB lotteries. Returns a tuple containing a dictionary with a sorted list of names or an error message and the session object.\n- **`scrape_nlb_latest_results(session, lottery_name, limit=5)`**: Fetch the latest NLB lottery results up to the specified limit. Requires a session from `scrape_nlb_active_lottery_names`. Returns a dictionary with a list of results.\n- **`scrape_dlb_latest_results(lottery_name, limit=5)`**: Fetch the latest DLB lottery results up to the specified limit. Returns a dictionary with a list of results.\n\n## Supported Lotteries\n\n### DLB Lotteries\nThe package supports the following DLB lotteries (as of the latest update):\n- Ada Kotipathi\n- Jayoda\n- Lagna Wasana\n- Sasiri\n- Shanida\n- Super Ball\n- Supiri Dhana Sampatha\n- Jaya Sampatha\n- Kapruka\n\n### NLB Lotteries\nThe package can scrape results for any active NLB lottery. Use `scrape_nlb_active_lottery_names()` to get the current list of active lotteries. Examples include:\n- Mega Power\n- Dhana Nidhanaya\n- Mahajana Sampatha\n- And more, depending on the active lotteries at the time of scraping.\n\n## Troubleshooting\n\n### Common Issues\n\n- **\"Result block not found\" or \"Request failed\" errors**:\n - Ensure you have a stable internet connection.\n - The NLB or DLB website structure may have changed. Check the website manually and consider updating the package or reporting an issue.\n - The draw number or date may not exist. Verify the input parameters.\n\n- **Cookie extraction failing**:\n - The `setCookie` function in the website\u2019s JavaScript may have changed. Report this issue to the package maintainers.\n\n- **Dependencies not installed**:\n - Run `pip install requests beautifulsoup4` to ensure dependencies are installed.\n\n### Debugging Tips\n\n- Enable verbose logging by modifying the code to print additional debug information (e.g., HTTP response status codes).\n- Check the raw HTML response if parsing fails using `print(response.text)` in the relevant function.\n- Test with different draw numbers or dates to isolate the issue.\n\n## FAQ\n\n### Q: What do I need to use this package?\nA: You need Python 3.6 or higher and the `requests` and `beautifulsoup4` libraries, which are automatically installed when you install the package via pip.\n\n### Q: Can I scrape results for any NLB lottery?\nA: Yes, as long as the lottery is active. Use `scrape_nlb_active_lottery_names()` to get the current list of active lotteries.\n\n### Q: Why am I getting an \"error\" key in the response?\nA: The error key indicates a problem, such as an invalid lottery name, unreachable website, or missing data. Check the error message for details and ensure your inputs are correct.\n\n### Q: Is this package legal to use?\nA: This package is designed for educational and personal use to scrape publicly available data. Ensure you comply with the terms of service of the NLB and DLB websites and applicable laws in your jurisdiction.\n\n### Q: How do I contribute to this package?\nA: Contributions are welcome! See the \"Contributing\" section below for details.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nWe welcome contributions to improve the `srilanka-lottery` package! To contribute:\n\n1. Fork the repository on GitHub.\n2. Create a new branch for your feature or bug fix.\n3. Make your changes and write tests if applicable.\n4. Submit a pull request with a clear description of your changes.\n\nPlease ensure your code follows the existing style and includes appropriate documentation. For major changes, open an issue first to discuss your proposal.\n\n## Contact\n\nFor questions, bug reports, or support, please contact [ic31908@gmail.com](mailto:ic31908@gmail.com) or open an issue on the [GitHub repository](https://github.com/ishanoshada/srilanka-lottery).\n\n## Acknowledgements\n\n- Built with [requests](https://requests.readthedocs.io/) and [BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup/).\n- Inspired by the need for accessible lottery data in Sri Lanka.\n- Thanks to the open-source community for providing tools and resources.\n\n**Repository Views** \n\n",
"bugtrack_url": null,
"license": null,
"summary": "Scrape Sri Lanka lottery results from NLB and DLB websites with this Python package",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/ishanoshada/srilanka-lottery"
},
"split_keywords": [
"lottery",
" sri lanka lottery",
" nlb",
" dlb",
" web scraping",
" lottery results",
" python lottery scraper",
" lottery data",
" national lottery board",
" development lottery board",
" lottery api",
" data extraction"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "e51327fd8648bfe3d533ab28cb34a93c8bbc1714d191dfacfd85e7b22c7da293",
"md5": "badcde8785b88556ae8eebaf5dbd06fc",
"sha256": "fec40dbeaa0f346f10ea545e4bbc80a3e5a32aeca81e0b68a0b38efb2ebb5c35"
},
"downloads": -1,
"filename": "srilanka_lottery-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "badcde8785b88556ae8eebaf5dbd06fc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9387,
"upload_time": "2025-08-16T18:16:13",
"upload_time_iso_8601": "2025-08-16T18:16:13.645848Z",
"url": "https://files.pythonhosted.org/packages/e5/13/27fd8648bfe3d533ab28cb34a93c8bbc1714d191dfacfd85e7b22c7da293/srilanka_lottery-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d1ac194bcf3600a5f18b32f35883337d13440c2d03f9056f0af6f11a99cd8e28",
"md5": "7b62f94d1ed14f1fa3a6691895235dc6",
"sha256": "49ffaf499ee15730beee7540b5eb0610e105dfaf5138550ea60a9bd433db4bba"
},
"downloads": -1,
"filename": "srilanka_lottery-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "7b62f94d1ed14f1fa3a6691895235dc6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12092,
"upload_time": "2025-08-16T18:16:14",
"upload_time_iso_8601": "2025-08-16T18:16:14.768469Z",
"url": "https://files.pythonhosted.org/packages/d1/ac/194bcf3600a5f18b32f35883337d13440c2d03f9056f0af6f11a99cd8e28/srilanka_lottery-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-16 18:16:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ishanoshada",
"github_project": "srilanka-lottery",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": [
[
">=",
"2.28.0"
]
]
},
{
"name": "beautifulsoup4",
"specs": [
[
">=",
"4.11.0"
]
]
}
],
"lcname": "srilanka-lottery"
}