/v1/crawls 端点,你可以爬取一个网站并获取所有页面的内容。
- 爬取一个网站并获取所有子页面的内容(或限制爬取的深度)
- 使用特殊模式爬取特定页面(例如
/blog/**) - 传递一个
webhook_url,在爬取完成时收到通知 - 搜索查询仅查找特定页面并按相关性排序
安装
pip install olostep
npm install olostep
# curl 在 macOS、Linux 和 Windows 上默认可用
npm install node-fetch
pip install requests
开始爬取
提供起始 URL,包含/排除 URL 模式,以及max_pages。可选:max_depth、include_external、include_subdomain、search_query、top_n、webhook_url、timeout。
from olostep import Olostep
client = Olostep(api_key="YOUR_REAL_KEY")
crawl = client.crawls.create(
start_url="https://olostep.com",
max_pages=100,
include_urls=["/**"],
exclude_urls=["/collections/**"],
include_external=False,
)
print(crawl.id, crawl.status)
import Olostep from 'olostep'
const client = new Olostep({ apiKey: 'YOUR_REAL_KEY' })
const crawl = await client.crawls.create({
url: 'https://olostep.com',
maxPages: 100,
includeUrls: ['/**'],
excludeUrls: ['/collections/**'],
includeExternal: false,
})
console.log(crawl.id, crawl.status)
curl -s -X POST "https://api.olostep.com/v1/crawls" \
-H "Authorization: Bearer $OLOSTEP_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"start_url": "https://olostep.com",
"max_pages": 100,
"include_urls": ["/**"],
"exclude_urls": ["/collections/**"]
}'
olostep crawl "https://olostep.com" \
--max-pages 100 \
--include-url "/**" \
--exclude-url "/collections/**" \
--formats markdown,html
const API_URL = 'https://api.olostep.com'
const res = await fetch(`${API_URL}/v1/crawls`, {
method: 'POST',
headers: { 'Authorization': 'Bearer <YOUR_API_KEY>', 'Content-Type': 'application/json' },
body: JSON.stringify({
start_url: 'https://olostep.com',
max_pages: 100,
include_urls: ['/**'],
exclude_urls: ['/collections/**']
})
})
console.log(await res.json())
import time, json
API_URL = 'https://api.olostep.com'
API_KEY = '<YOUR_API_KEY>'
HEADERS = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {API_KEY}'
}
data = {
"start_url": "https://olostep.com",
"max_pages": 100,
"include_urls": ["/**"],
"exclude_urls": ["/collections/**"],
"include_external": False
}
res = requests.post(f"{API_URL}/v1/crawls", headers=HEADERS, json=data)
crawl = res.json()
print(json.dumps(crawl, indent=2))
crawl 对象作为响应。crawl 对象有一些属性,比如 id 和 status,你可以用来跟踪爬取。
检查爬取状态
轮询爬取以跟踪进度,直到status 为 completed。
# 使用上一步的爬取对象
info = crawl.info()
print(info.status, info.pages_count)
# 或等待完成
crawl.wait_till_done(check_every_n_secs=5)
// 使用上一步的爬取对象
const info = await crawl.info()
console.log(info.status, info.pages_count)
// 或等待完成
await crawl.waitTillDone({ checkEveryNSecs: 5 })
curl -s -X GET "https://api.olostep.com/v1/crawls/<CRAWL_ID>" \
-H "Authorization: Bearer $OLOSTEP_API_KEY"
const crawlId = '<CRAWL_ID>'
const status = await fetch(`${API_URL}/v1/crawls/${crawlId}`, {
headers: { 'Authorization': 'Bearer <YOUR_API_KEY>' }
}).then(r => r.json())
console.log(status)
import time
def get_crawl_info(crawl_id):
return requests.get(f'{API_URL}/v1/crawls/{crawl_id}', headers=HEADERS).json()
crawl_id = crawl['id']
while True:
info = get_crawl_info(crawl_id)
print(info['status'], info.get('pages_count'))
if info['status'] == 'completed':
break
time.sleep(5)
webhook_url,以便在爬取完成时收到通知。
列出页面(使用游标分页/流式处理)
获取页面并使用cursor 和 limit 进行迭代。适用于爬取 in_progress 或 completed 状态。
# 迭代所有页面(自动等待爬取完成,处理分页)
for page in crawl.pages():
print(page.url, page.retrieve_id)
// 迭代所有页面(自动等待爬取完成,处理分页)
for await (const page of crawl.pages()) {
console.log(page.url, page.retrieve_id)
}
curl -s -G "https://api.olostep.com/v1/crawls/<CRAWL_ID>/pages" \
-H "Authorization: Bearer $OLOSTEP_API_KEY" \
--data-urlencode "cursor=0" \
--data-urlencode "limit=10"
let cursor = 0
while (true) {
const pages = await fetch(`${API_URL}/v1/crawls/${crawlId}/pages?cursor=${cursor}&limit=10`, {
headers: { 'Authorization': 'Bearer <YOUR_API_KEY>' }
}).then(r => r.json())
pages.pages.forEach(p => console.log(p.url, p.retrieve_id))
if (pages.cursor === undefined) break
cursor = pages.cursor
}
def get_pages(crawl_id, cursor=None, limit=10, search_query=None):
params = {
'cursor': cursor,
'limit': limit
}
return requests.get(f'{API_URL}/v1/crawls/{crawl_id}/pages', headers=HEADERS, params=params).json()
cursor = 0
while True:
page_batch = get_pages(crawl_id, cursor=cursor, limit=10)
for page in page_batch['pages']:
print(page['url'], page['retrieve_id'])
if 'cursor' not in page_batch:
break
cursor = page_batch['cursor']
time.sleep(5)
搜索查询(限制为前 N 个相关)
在开始时使用search_query,并可选择用 search_query 过滤列表。使用 top_n 限制每页探索。
from olostep import Olostep
client = Olostep(api_key="YOUR_REAL_KEY")
crawl = client.crawls.create(
start_url="https://olostep.com",
max_pages=100,
include_urls=["/**"],
search_query="contact us",
top_n=5,
)
for page in crawl.pages(search_query="contact us"):
print(page.url)
import Olostep from 'olostep'
const client = new Olostep({ apiKey: 'YOUR_REAL_KEY' })
const crawl = await client.crawls.create({
url: 'https://olostep.com',
maxPages: 100,
includeUrls: ['/**'],
searchQuery: 'contact us',
topN: 5,
})
for await (const page of crawl.pages()) {
console.log(page.url)
}
curl -s -X POST "https://api.olostep.com/v1/crawls" \
-H "Authorization: Bearer $OLOSTEP_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"start_url": "https://olostep.com",
"max_pages": 100,
"include_urls": ["/**"],
"search_query": "contact us",
"top_n": 5
}'
olostep crawl "https://olostep.com" \
--max-pages 100 \
--include-url "/**" \
--search-query "contact us" \
--top-n 5
await fetch(`${API_URL}/v1/crawls`, { method: 'POST', headers: { 'Authorization': 'Bearer <YOUR_API_KEY>', 'Content-Type': 'application/json' }, body: JSON.stringify({ start_url: 'https://olostep.com', max_pages: 100, include_urls: ['/**'], search_query: 'contact us', top_n: 5 }) })
data = {
"start_url": "https://olostep.com",
"max_pages": 100,
"include_urls": ["/**"],
"search_query": "contact us",
"top_n": 5
}
crawl = requests.post(f'{API_URL}/v1/crawls', headers=HEADERS, json=data).json()
pages = requests.get(f"{API_URL}/v1/crawls/{crawl['id']}/pages", headers=HEADERS, params={'search_query': 'contact us'}).json()
print(len(pages['pages']))
获取内容
使用每个页面的retrieve_id 和 /v1/retrieve 来获取 html_content 和/或 markdown_content。
# 获取每个爬取页面的内容
for page in crawl.pages():
content = page.retrieve(["markdown"])
print(content.markdown_content)
// 获取每个爬取页面的内容
for await (const page of crawl.pages()) {
const content = await client.retrieve.get(page.retrieve_id, ['markdown'])
console.log(content.markdown_content)
}
curl -s -G "https://api.olostep.com/v1/retrieve" \
-H "Authorization: Bearer $OLOSTEP_API_KEY" \
--data-urlencode "retrieve_id=<RETRIEVE_ID>"
# `olostep crawl` 自动检索内容。要稍后通过 retrieve_id 获取单个页面:
olostep scrape-get <RETRIEVE_ID>
const retrieved = await fetch(`${API_URL}/v1/retrieve?retrieve_id=<RETRIEVE_ID>`, { headers: { 'Authorization': 'Bearer <YOUR_API_KEY>' } }).then(r => r.json())
console.log(retrieved.markdown_content)
def retrieve_content(retrieve_id):
return requests.get(f"{API_URL}/v1/retrieve", headers=HEADERS, params={"retrieve_id": retrieve_id}).json()
for page in get_pages(crawl['id'], limit=5)['pages']:
retrieved = retrieve_content(page['retrieve_id'])
print(retrieved.get('markdown_content'))
注意事项
- 分页是基于游标的;重复请求直到
cursor缺失。 /v1/crawls/{crawl_id}/pages上的内容字段已弃用;优先使用/v1/retrieve。- Webhooks:设置
webhook_url以在爬取完成时接收 POST。
存储
爬取的存储文件默认保留 7 天。在POST /v1/crawls 时传递一个 storage 对象以保持更长时间或无限期。该设置会传递给爬取中的每个页面抓取,因此它涵盖了你稍后用 retrieve_id 获取的内容。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
storage.expires_in | string | 7d | 存储文件的保留时间。可选值:7d、10d、30d、60d、90d、180d、365d 或 never(无限期)。 |
storage,或发送不带 expires_in 的 storage,使用 7d。创建响应和 GET /v1/crawls/{crawl_id} 都包含生效的值:
"storage": { "expires_in": "30d" }
cURL
curl -s -X POST "https://api.olostep.com/v1/crawls" \
-H "Authorization: Bearer $OLOSTEP_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"start_url": "https://example.com",
"max_pages": 10,
"storage": { "expires_in": "30d" }
}'
400:
{
"type": "invalid_request_error",
"message": "Invalid storage.expires_in. Allowed values: \"7d\", \"10d\", \"30d\", \"60d\", \"90d\", \"180d\", \"365d\", \"never\".",
"code": "invalid_storage_expires_in"
}