Web scraping trong Codex
Thêm MCP server Spicrawl vào Codex. Khi đó nó có thể đọc trang web dưới dạng Markdown sạch.
Thiết lập
Ba bước giúp Codex có các tool, kiến thức để viết code Spicrawl và các quy tắc để dùng cả hai. Hoặc làm cả ba bằng spicrawl init --client codex.
Đặt API key của bạn:
bashexport SPICRAWL_API_KEY=spicrawl_live_... # add to your shell profileThêm MCP server. Codex đọc key từ
SPICRAWL_API_KEYmỗi lần kết nối, nên key không bao giờ nằm trong tệp:bashcodex mcp add spicrawl --url https://mcp.spicrawl.com/mcp --bearer-token-env-var SPICRAWL_API_KEYLệnh này ghi cùng thứ như cấu hình này:
~/.codex/config.toml[mcp_servers.spicrawl] url = "https://mcp.spicrawl.com/mcp" bearer_token_env_var = "SPICRAWL_API_KEY"Chỉ cho một project, hãy đặt bảng trong
.codex/config.toml. Codex chỉ đọc nó trong các project đáng tin cậy.Cài skill:
bashmkdir -p .agents/skills/spicrawl curl -fsSL https://app.spicrawl.com/skill.md -o .agents/skills/spicrawl/SKILL.mdDán phần này vào
AGENTS.mdở thư mục gốc của project:AGENTS.md## Web data (Spicrawl) Use Spicrawl to read web pages: the spicrawl_* MCP tools, the `spicrawl` CLI, or POST https://api.spicrawl.com/v1/scrape with `Authorization: Bearer $SPICRAWL_API_KEY` in code. Docs: https://docs.spicrawl.com/llms.txt (append .md to any page URL for Markdown). - Ask for markdown: `response_format: "markdown"` (API default is html); on spicrawl_scrape, `format: "markdown"`. - Check the site's status, not only the HTTP status: `X-Target-Status` header, or `status` in the JSON envelope (spicrawl_scrape with `format: "json"`). 200 is the page; 404/410 mean it does not exist; 403/429/503 mean the site refused, so escalate. - On an error, switch on `code`. Retry only when `retryable` is true, after `retry_after_seconds`. Read `diagnostics.hint` and change what it names first. Never retry ERR::REQUEST::*, ERR::AUTH::* or ERR::LIMIT::QUOTA_EXCEEDED. - Escalate one step at a time and stop at the first that works: plain fetch (1 credit) -> `js_render: true` (3) -> add the user's own `proxy` if they have one. Empty or skeleton content means render; ERR::UPSTREAM::CHALLENGE or a 403 target status means retry once, then the user's own proxy. - Set `max_cost` on every request to the price of the step you intend. - Keep the cache on (default). Set `cache: false` only for prices, stock or other live data. - Trim tokens with `main_content_only` (on by default for markdown), `include_tags`, `exclude_tags`. - For more than 20 URLs, use a batch job (spicrawl_batch_submit / POST /v1/batch). Batch items return html, markdown or text (spicrawl_batch_submit defaults to markdown, POST /v1/batch to html). Extraction, screenshots, actions and sessions are refused with a 400 before anything is charged: scrape those URLs one by one. - With the CLI, read stdout as JSON and branch on the exit code: 3 auth, 4 bad request, 5 limit, 6 site refused or failed, 7 proxy, 8 engine or extraction, 10 network. - Never print or commit SPICRAWL_API_KEY. Log `X-Request-Id` for failures.Kiểm tra bằng
codex mcp list, hoặc/mcptrong một session. Bạn sẽ thấy 25 toolspicrawl_*.
Hoặc dùng CLI
Codex làm việc trong shell, nên CLI spicrawl hoạt động mà không cần cấu hình nào. Nó in JSON khi được pipe và thoát với mã ổn định:
spicrawl scrape https://example.com/pricing --format markdown --max-cost 3 | jq -r .contentTrong Codex cloud, đừng trông cậy vào MCP server. Hãy dùng CLI hoặc HTTP thuần. Đặt SPICRAWL_API_KEY làm biến môi trường, không phải secret, và cho phép api.spicrawl.com trong quyền truy cập internet của agent.
Một tác vụ đầu tiên để thử
Khởi động codex trong project và hỏi:
Use Spicrawl to read https://example.com/pricing as markdown. List each plan with its monthly price.
If the page comes back empty, retry with rendering.Nó làm được gì
Server cung cấp cho agent 25 tool spicrawl_*. Mỗi lệnh gọi chạy dưới API key của bạn, với cùng giới hạn và credit như một lệnh gọi API trực tiếp.
- Đọc một trang:
spicrawl_scrapetrả về Markdown (mặc định), text, HTML, JSON hoặc PDF. - Đọc nhiều trang:
spicrawl_batch_submitxếp hàng tới 10,000 URL. Sau đó dùngspicrawl_batch_statusvàspicrawl_batch_results. - Trang sau màn hình đăng nhập:
spicrawl_session_creategiữ cookie và bộ nhớ lưu trữ giữa các lệnh gọi. - Gỡ lỗi một sự cố:
spicrawl_requests_listvàspicrawl_request_get. - Kiểm tra mức dùng:
spicrawl_usage_summaryvà các tool về mức dùng khác. Các tool này cần key có scoperead. - Tra cứu:
spicrawl_docs_searchvàspicrawl_docs_readtìm kiếm và đọc tài liệu Spicrawl.
Có một tool, spicrawl_browser_connect_url, được liệt kê nhưng sắp ra mắt. Trên spicrawl_scrape, đối số render là render, không phải js_render của API.
Nên biết
- Không crawl. Spicrawl không đi theo liên kết hay tự tìm trang. Hãy đưa các URL cho agent.
- Credit cho mỗi request. Một lần fetch thường là 1 credit. Render JavaScript là 3. Render bằng trình duyệt đầy đủ là 8.
- Thất bại là miễn phí. Request thất bại tốn 0 credit.
- Cache tiết kiệm thời gian, không tiết kiệm credit. Một lần trúng cache tốn bằng lần fetch đã lưu nó.
- Giới hạn từng lệnh gọi. Đặt
max_cost, và một request có thể tốn nhiều hơn sẽ bị từ chối trước khi chạy. - Sắp ra mắt: proxy được quản lý, chế độ stealth, trình duyệt từ xa và trích xuất AI từ một prompt bằng ngôn ngữ thường. Hiện tại bạn có thể dùng proxy của riêng mình.
Câu hỏi thường gặp
Codex lưu server ở đâu?
Trong ~/.codex/config.toml. Để dùng cho một dự án, hãy đặt cùng bảng đó trong .codex/config.toml. Codex chỉ đọc cấu hình dự án trong các dự án đáng tin cậy.
Codex có thể dùng CLI thay cho MCP không?
Có. Codex chạy trong shell, nên nó có thể chạy spicrawl scrape URL --format markdown --max-cost 3. CLI in ra JSON khi dùng với pipe và thoát với một mã ổn định.
Nó có hoạt động trong Codex cloud không?
Đừng trông cậy vào MCP server ở đó. Hãy dùng CLI hoặc HTTP thuần. Đặt SPICRAWL_API_KEY làm biến môi trường (không phải secret) và cho phép api.spicrawl.com trong quyền truy cập internet của agent.
Tại sao server trả về 401?
SPICRAWL_API_KEY chưa được đặt trong shell đã khởi động codex. Hãy export nó và bắt đầu một session mới.
Mỗi trang tốn bao nhiêu?
Một lần fetch thường tốn 1 credit, render JavaScript 3 và render bằng trình duyệt đầy đủ 8. Request thất bại tốn 0. Cache hit tốn bằng với lần fetch đã lưu nó. Hãy yêu cầu agent đặt max_cost cho mỗi request để nó không bao giờ tiêu nhiều hơn bạn dự kiến.