Lightweight async data gathering for Python
Project description
DataService
Lightweight - async - data gathering for Python.
DataService is a lightweight web scraping and general purpose data gathering library for Python.
Designed for simplicity, it’s built upon common web scraping and data gathering patterns.
No complex API to learn, just standard Python idioms.
Dual synchronous and asynchronous support.
Installation
Please note that DataService requires Python 3.11 or higher.
You can install DataService via pip:
pip install python-dataservice
You can also install the optional playwright dependency to use the PlaywrightClient:
pip install python-dataservice[playwright]
To install Playwright, run:
python -m playwright install
or simply:
playwright install
How to use DataService
To start, create a DataService instance with an Iterable of Request objects. This setup provides you with an Iterator of data objects that you can then iterate over or convert to a list, tuple, a pd.DataFrame or any data structure of choice.
start_requests = [Request(url="https://books.toscrape.com/index.html", callback=parse_books_page, client=HttpXClient())]
data_service = DataService(start_requests)
data = tuple(data_service)
A Request is a Pydantic model that includes the URL to fetch, a reference to the client callable, and a callback function for parsing the Response object.
The client can be any async Python callable that accepts a Request object and returns a Response object. DataService provides an HttpXClient class by default, which is based on the httpx library, but you are free to use your own custom async client.
The callback function processes a Response object and returns either data or additional Request objects.
In this trivial example we are requesting the Books to Scrape homepage and parsing the number of books on the page.
Example parse_books_page function:
def parse_books_page(response: Response):
articles = response.html.find_all("article", {"class": "product_pod"})
return {
"url": response.url,
"title": response.html.title.get_text(strip=True),
"articles": len(articles),
}
This function takes a Response object, which has a html attribute (a BeautifulSoup object of the HTML content). The function parses the HTML content and returns data.
The callback function can return or yield either data (dict or pydantic.BaseModel) or more Request objects.
If you have used Scrapy before, you will find this pattern familiar.
For more examples and advanced usage, check out the examples section.
For a detailed API reference, check out the API section.
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
File details
Details for the file python_dataservice-0.10.0.tar.gz
.
File metadata
- Download URL: python_dataservice-0.10.0.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cd68d07d4b2cb3b686334c05f57b6c8601234da7520773377fcb5df7f149d42 |
|
MD5 | a039601a51b43490f41c51055345ee78 |
|
BLAKE2b-256 | 102319bc3d119abe9e0576e2551c6fe89f99100e6d89ed236e5e45be796ec88b |
Provenance
File details
Details for the file python_dataservice-0.10.0-py3-none-any.whl
.
File metadata
- Download URL: python_dataservice-0.10.0-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 072339a99b901fb5338f3664e5f2d44b637e5eac0ce7c9f60052b09b5f8e9f1f |
|
MD5 | ec5307aa8a52ee233c83f155c003f0c6 |
|
BLAKE2b-256 | 67afda1200ad02265987dd6aa7d5a534cb2abc1a3a5ede8ec868ba1da5ea7b50 |