同步完整源码 - 2026-05-24
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from crawlee.storage_clients import StorageClient
|
||||
from crawlee.storage_clients._base import (
|
||||
DatasetClient,
|
||||
KeyValueStoreClient,
|
||||
RequestQueueClient,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from crawlee.configuration import Configuration
|
||||
|
||||
# Implement the storage type clients with your backend logic.
|
||||
|
||||
|
||||
class CustomDatasetClient(DatasetClient):
|
||||
# Implement methods like push_data, get_data, iterate_items, etc.
|
||||
pass
|
||||
|
||||
|
||||
class CustomKeyValueStoreClient(KeyValueStoreClient):
|
||||
# Implement methods like get_value, set_value, delete, etc.
|
||||
pass
|
||||
|
||||
|
||||
class CustomRequestQueueClient(RequestQueueClient):
|
||||
# Implement methods like add_request, fetch_next_request, etc.
|
||||
pass
|
||||
|
||||
|
||||
# Implement the storage client factory.
|
||||
|
||||
|
||||
class CustomStorageClient(StorageClient):
|
||||
async def create_dataset_client(
|
||||
self,
|
||||
*,
|
||||
id: str | None = None,
|
||||
name: str | None = None,
|
||||
configuration: Configuration | None = None,
|
||||
) -> CustomDatasetClient:
|
||||
# Create and return your custom dataset client.
|
||||
pass
|
||||
|
||||
async def create_kvs_client(
|
||||
self,
|
||||
*,
|
||||
id: str | None = None,
|
||||
name: str | None = None,
|
||||
configuration: Configuration | None = None,
|
||||
) -> CustomKeyValueStoreClient:
|
||||
# Create and return your custom key-value store client.
|
||||
pass
|
||||
|
||||
async def create_rq_client(
|
||||
self,
|
||||
*,
|
||||
id: str | None = None,
|
||||
name: str | None = None,
|
||||
configuration: Configuration | None = None,
|
||||
) -> CustomRequestQueueClient:
|
||||
# Create and return your custom request queue client.
|
||||
pass
|
||||
@@ -0,0 +1,8 @@
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import FileSystemStorageClient
|
||||
|
||||
# Create a new instance of storage client.
|
||||
storage_client = FileSystemStorageClient()
|
||||
|
||||
# And pass it to the crawler.
|
||||
crawler = ParselCrawler(storage_client=storage_client)
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
from crawlee.configuration import Configuration
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import FileSystemStorageClient
|
||||
|
||||
# Create a new instance of storage client.
|
||||
storage_client = FileSystemStorageClient()
|
||||
|
||||
# Create a configuration with custom settings.
|
||||
configuration = Configuration(
|
||||
storage_dir='./my_storage',
|
||||
purge_on_start=False,
|
||||
)
|
||||
|
||||
# And pass them to the crawler.
|
||||
crawler = ParselCrawler(
|
||||
storage_client=storage_client,
|
||||
configuration=configuration,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import MemoryStorageClient
|
||||
|
||||
# Create a new instance of storage client.
|
||||
storage_client = MemoryStorageClient()
|
||||
|
||||
# And pass it to the crawler.
|
||||
crawler = ParselCrawler(storage_client=storage_client)
|
||||
@@ -0,0 +1,10 @@
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import RedisStorageClient
|
||||
|
||||
# Create a new instance of storage client using connection string.
|
||||
# 'redis://localhost:6379' is the just placeholder, replace it with your actual
|
||||
# connection string.
|
||||
storage_client = RedisStorageClient(connection_string='redis://localhost:6379')
|
||||
|
||||
# And pass it to the crawler.
|
||||
crawler = ParselCrawler(storage_client=storage_client)
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
from redis.asyncio import Redis
|
||||
|
||||
from crawlee.configuration import Configuration
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import RedisStorageClient
|
||||
|
||||
# Create a new instance of storage client using a Redis client with custom settings.
|
||||
# Replace host and port with your actual Redis server configuration.
|
||||
# Other Redis client settings can be adjusted as needed.
|
||||
storage_client = RedisStorageClient(
|
||||
redis=Redis(
|
||||
host='localhost',
|
||||
port=6379,
|
||||
retry_on_timeout=True,
|
||||
socket_keepalive=True,
|
||||
socket_connect_timeout=10,
|
||||
)
|
||||
)
|
||||
|
||||
# Create a configuration with custom settings.
|
||||
configuration = Configuration(purge_on_start=False)
|
||||
|
||||
# And pass them to the crawler.
|
||||
crawler = ParselCrawler(
|
||||
storage_client=storage_client,
|
||||
configuration=configuration,
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee import service_locator
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import MemoryStorageClient
|
||||
from crawlee.storages import Dataset
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create custom storage client, MemoryStorageClient for example.
|
||||
storage_client = MemoryStorageClient()
|
||||
|
||||
# Register it globally via the service locator.
|
||||
service_locator.set_storage_client(storage_client)
|
||||
|
||||
# Or pass it directly to the crawler, it will be registered globally
|
||||
# to the service locator under the hood.
|
||||
crawler = ParselCrawler(storage_client=storage_client)
|
||||
|
||||
# Or just provide it when opening a storage (e.g. dataset), it will be used
|
||||
# for this storage only, not globally.
|
||||
dataset = await Dataset.open(
|
||||
name='my-dataset',
|
||||
storage_client=storage_client,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,12 @@
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import SqlStorageClient
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create a new instance of storage client.
|
||||
# This will create an SQLite database file crawlee.db or created tables in your
|
||||
# database if you pass `connection_string` or `engine`
|
||||
# Use the context manager to ensure that connections are properly cleaned up.
|
||||
async with SqlStorageClient() as storage_client:
|
||||
# And pass it to the crawler.
|
||||
crawler = ParselCrawler(storage_client=storage_client)
|
||||
@@ -0,0 +1,33 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from crawlee.configuration import Configuration
|
||||
from crawlee.crawlers import ParselCrawler
|
||||
from crawlee.storage_clients import SqlStorageClient
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create a new instance of storage client.
|
||||
# On first run, also creates tables in your PostgreSQL database.
|
||||
# Use the context manager to ensure that connections are properly cleaned up.
|
||||
async with SqlStorageClient(
|
||||
# Create an `engine` with the desired configuration
|
||||
engine=create_async_engine(
|
||||
'postgresql+asyncpg://myuser:mypassword@localhost:5432/postgres',
|
||||
future=True,
|
||||
pool_size=5,
|
||||
max_overflow=10,
|
||||
pool_recycle=3600,
|
||||
pool_pre_ping=True,
|
||||
echo=False,
|
||||
)
|
||||
) as storage_client:
|
||||
# Create a configuration with custom settings.
|
||||
configuration = Configuration(
|
||||
purge_on_start=False,
|
||||
)
|
||||
|
||||
# And pass them to the crawler.
|
||||
crawler = ParselCrawler(
|
||||
storage_client=storage_client,
|
||||
configuration=configuration,
|
||||
)
|
||||
Reference in New Issue
Block a user