同步完整源码 - 2026-05-24
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.configuration import Configuration
|
||||
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Set the purge_on_start field to False to avoid purging the storage on start.
|
||||
# highlight-next-line
|
||||
configuration = Configuration(purge_on_start=False)
|
||||
|
||||
# Pass the configuration to the crawler.
|
||||
crawler = HttpCrawler(configuration=configuration)
|
||||
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: HttpCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
await crawler.run(['https://crawlee.dev/'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,20 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.storages import Dataset
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create storage client with configuration
|
||||
dataset = await Dataset.open(name='my-dataset')
|
||||
|
||||
# Purge the dataset explicitly - purging will remove all items from the dataset.
|
||||
# But keeps the dataset itself and its metadata.
|
||||
await dataset.purge()
|
||||
|
||||
# Or you can drop the dataset completely, which will remove the dataset
|
||||
# and all its items.
|
||||
await dataset.drop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.storages import Dataset
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the dataset, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default dataset.
|
||||
dataset = await Dataset.open(name='my-dataset')
|
||||
|
||||
# Push a single row of data.
|
||||
await dataset.push_data({'foo': 'bar'})
|
||||
|
||||
# Push multiple rows of data (anything JSON-serializable can be pushed).
|
||||
await dataset.push_data([{'foo': 'bar2', 'col2': 'val2'}, {'col3': 123}])
|
||||
|
||||
# Fetch all data from the dataset.
|
||||
data = await dataset.get_data()
|
||||
# Do something with it...
|
||||
|
||||
# Remove the dataset.
|
||||
await dataset.drop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,32 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import BeautifulSoupCrawler, BeautifulSoupCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create a new crawler (it can be any subclass of BasicCrawler).
|
||||
crawler = BeautifulSoupCrawler()
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: BeautifulSoupCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# Extract data from the page.
|
||||
data = {
|
||||
'url': context.request.url,
|
||||
'title': context.soup.title.string if context.soup.title else None,
|
||||
}
|
||||
|
||||
# Push the extracted data to the (default) dataset.
|
||||
await context.push_data(data)
|
||||
|
||||
# Run the crawler with the initial URLs.
|
||||
await crawler.run(['https://crawlee.dev'])
|
||||
|
||||
# Export the dataset to a file.
|
||||
await crawler.export_data(path='dataset.csv')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import BeautifulSoupCrawler, BeautifulSoupCrawlingContext
|
||||
from crawlee.storages import Dataset
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the dataset, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default dataset.
|
||||
dataset = await Dataset.open(name='my-dataset')
|
||||
|
||||
# Create a new crawler (it can be any subclass of BasicCrawler).
|
||||
crawler = BeautifulSoupCrawler()
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: BeautifulSoupCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# Extract data from the page.
|
||||
data = {
|
||||
'url': context.request.url,
|
||||
'title': context.soup.title.string if context.soup.title else None,
|
||||
}
|
||||
|
||||
# Push the extracted data to the dataset.
|
||||
await dataset.push_data(data)
|
||||
|
||||
# Run the crawler with the initial URLs.
|
||||
await crawler.run(['https://crawlee.dev'])
|
||||
|
||||
# Export the dataset to the key-value store.
|
||||
await dataset.export_to(key='dataset', content_type='csv')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,19 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import BeautifulSoupCrawler, BeautifulSoupCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
crawler = BeautifulSoupCrawler()
|
||||
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: BeautifulSoupCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
# highlight-next-line
|
||||
await context.add_requests(['https://apify.com/'])
|
||||
|
||||
await crawler.run(['https://crawlee.dev/'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,19 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import BeautifulSoupCrawler, BeautifulSoupCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
crawler = BeautifulSoupCrawler()
|
||||
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: BeautifulSoupCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
# highlight-next-line
|
||||
await context.enqueue_links()
|
||||
|
||||
await crawler.run(['https://crawlee.dev/'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.storages import KeyValueStore
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the key-value store, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default KVS.
|
||||
kvs = await KeyValueStore.open(name='my-key-value-store')
|
||||
|
||||
# Set a value associated with 'some-key'.
|
||||
await kvs.set_value(key='some-key', value={'foo': 'bar'})
|
||||
|
||||
# Get the value associated with 'some-key'.
|
||||
value = kvs.get_value('some-key')
|
||||
# Do something with it...
|
||||
|
||||
# Delete the value associated with 'some-key' by setting it to None.
|
||||
await kvs.set_value(key='some-key', value=None)
|
||||
|
||||
# Remove the key-value store.
|
||||
await kvs.drop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,35 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create a new Playwright crawler.
|
||||
crawler = PlaywrightCrawler()
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: PlaywrightCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# Capture the screenshot of the page using Playwright's API.
|
||||
screenshot = await context.page.screenshot()
|
||||
name = context.request.url.split('/')[-1]
|
||||
|
||||
# Get the key-value store from the context. # If it does not exist,
|
||||
# it will be created. Leave name empty to use the default KVS.
|
||||
kvs = await context.get_key_value_store()
|
||||
|
||||
# Store the screenshot in the key-value store.
|
||||
await kvs.set_value(
|
||||
key=f'screenshot-{name}',
|
||||
value=screenshot,
|
||||
content_type='image/png',
|
||||
)
|
||||
|
||||
# Run the crawler with the initial URLs.
|
||||
await crawler.run(['https://crawlee.dev'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext
|
||||
from crawlee.storages import KeyValueStore
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the key-value store, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default KVS.
|
||||
kvs = await KeyValueStore.open(name='my-key-value-store')
|
||||
|
||||
# Create a new Playwright crawler.
|
||||
crawler = PlaywrightCrawler()
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: PlaywrightCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# Capture the screenshot of the page using Playwright's API.
|
||||
screenshot = await context.page.screenshot()
|
||||
name = context.request.url.split('/')[-1]
|
||||
|
||||
# Store the screenshot in the key-value store.
|
||||
await kvs.set_value(
|
||||
key=f'screenshot-{name}',
|
||||
value=screenshot,
|
||||
content_type='image/png',
|
||||
)
|
||||
|
||||
# Run the crawler with the initial URLs.
|
||||
await crawler.run(['https://crawlee.dev'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,18 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.storages import Dataset
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Named storage (persists across runs)
|
||||
dataset_named = await Dataset.open(name='my-persistent-dataset')
|
||||
|
||||
# Unnamed storage with alias (purged on start)
|
||||
dataset_unnamed = await Dataset.open(alias='temporary-results')
|
||||
|
||||
# Default unnamed storage (purged on start)
|
||||
dataset_default = await Dataset.open()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,31 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.storages import RequestQueue
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the request queue, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default request queue.
|
||||
request_queue = await RequestQueue.open(name='my-request-queue')
|
||||
|
||||
# Add a single request.
|
||||
await request_queue.add_request('https://apify.com/')
|
||||
|
||||
# Add multiple requests as a batch.
|
||||
await request_queue.add_requests(
|
||||
['https://crawlee.dev/', 'https://crawlee.dev/python/']
|
||||
)
|
||||
|
||||
# Fetch and process requests from the queue.
|
||||
while request := await request_queue.fetch_next_request():
|
||||
# Do something with it...
|
||||
|
||||
# And mark it as handled.
|
||||
await request_queue.mark_request_as_handled(request)
|
||||
|
||||
# Remove the request queue.
|
||||
await request_queue.drop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,27 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Create a new crawler (it can be any subclass of BasicCrawler). Request queue is
|
||||
# a default request manager, it will be opened, and fully managed if not specified.
|
||||
crawler = HttpCrawler()
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: HttpCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# Use context's add_requests method helper to add new requests from the handler.
|
||||
await context.add_requests(['https://crawlee.dev/python/'])
|
||||
|
||||
# Use crawler's add_requests method helper to add new requests.
|
||||
await crawler.add_requests(['https://apify.com/'])
|
||||
|
||||
# Run the crawler. You can optionally pass the list of initial requests.
|
||||
await crawler.run(['https://crawlee.dev/'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
|
||||
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
|
||||
from crawlee.storages import RequestQueue
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Open the request queue, if it does not exist, it will be created.
|
||||
# Leave name empty to use the default request queue.
|
||||
request_queue = await RequestQueue.open(name='my-request-queue')
|
||||
|
||||
# Interact with the request queue directly, e.g. add a batch of requests.
|
||||
await request_queue.add_requests(['https://apify.com/', 'https://crawlee.dev/'])
|
||||
|
||||
# Create a new crawler (it can be any subclass of BasicCrawler) and pass the request
|
||||
# queue as request manager to it. It will be managed by the crawler.
|
||||
crawler = HttpCrawler(request_manager=request_queue)
|
||||
|
||||
# Define the default request handler, which will be called for every request.
|
||||
@crawler.router.default_handler
|
||||
async def request_handler(context: HttpCrawlingContext) -> None:
|
||||
context.log.info(f'Processing {context.request.url} ...')
|
||||
|
||||
# And execute the crawler.
|
||||
await crawler.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user