使用 IronWebScraper 搭配 AI 程式輔助工具

Curtis Chau
Curtis Chau
已更新: 2026年10月26日

IronWebScraper 是 Iron Software 的 .NET 網頁擷取程式庫。 它能爬行網站、在頁面之間跟隨連結,並從 C# 中的 HTML 擷取結構化數據,而且單一套件能涵蓋所有運行 .NET Standard 2.0 的平台。

程式輔助工具可以從一個請求生成 .NET 網頁擷取工具。檢查的工作在於這段代碼是否尊重其目標網站,以及它在第二頁中是否仍能正常運作。 記憶體中的輔助工具能寫出原始 HttpClient 迴圈,增添其自己的重試邏輯,並在連接速度允許下快速發送請求。 IronWebScraper 已管理請求佇列、調節和身份認證,因此輔助工具只需編寫一個類,其中有兩個覆蓋方法即可。 本指南測試運行中的輔助工具根據可用文件撰寫了一個 WebScraper 子類,並未進行 HttpClient 呼叫。

每個助手的設置過程都是一樣的。 添加套件,從環境中指定金鑰,為輔助工具提供 IronWebScraper 的機器可讀文件,並在標記工作完成之前比較爬行器的輸出與網站的請求。 這適用於 基本擷取教程、博客擷取、購物網站擷取和高級設置。

Claude Code AI 編碼助理標誌ChatGPT Codex AI 編碼助理標誌GitHub Copilot AI 程式碼助理標誌Cursor AI 程式碼編輯器標誌Google Antigravity AI 程式碼代理標誌

我的 AI 助手不能自己寫出擷取工具嗎?

它可以寫一個。 困難出現在程式之外,出現在目標網站接收到的內容和輸出文件所包含的內容中。

  • 運行的擷取工具仍可能導致站點過載: RateLimitPerHost 設定對單一主機的請求間隔最小延遲,OpenConnectionLimitPerHost 限制並行連接數。
  • HTML 選擇不必用正則表達式: Response 提供 Css、XPath、QuerySelector 和 GetElementById。
  • 準確命名套件: 套件和命名空間均為 IronWebScraper。 攻擊者註冊輔助工具可能錯誤生成的套件名稱,這種做法稱為 slopsquatting。
  • 授權條款事先定義: 授權頁面列出每層允許的開發者人數。

步驟 1:安裝 IronWebScraper

Add the IronWebScraper NuGet package, version 2026.10.1, to this project and restore it.
Tell me which IronWebScraper version the project file lists now.
Text

從終端,顯式固定版本。

dotnet add package IronWebScraper --version 2026.10.1
SHELL

套件不包含瀏覽器引擎和本機二進位文件。 它唯一的依賴是 IronSoftware.Common 和 IronSoftware.System.Drawing,第二個帶來 SixLabors.ImageSharp 3.1.12。在此版本中,dotnet list package --vulnerable --include-transitive 報告了五個警告,其中三個為高風險。


步驟 2:套用您的許可證金鑰

沒有金鑰,Start 在爬行器發送一個請求之前便拋出 IronSoftware.Exceptions.LicensingException,且沒有任何內容達到輸出資料夾。 消息提到七天的免費開發使用,但本探測首次調用即出現例外。 鍵入錯誤的金鑰會顯示 "未識別" 消息,且不重複顯示金鑰。 下面提供了一個免費試用密鑰。

立即在您的項目中開始使用 IronWebScraper,免費試用。

第一步:
arrow pointer

在應用程序啟動時指派一次授權金鑰,在首次擷取前。 從環境變數中檢索它,這樣它就不會存儲在存儲庫中。

IronWebScraper.License.LicenseKey = Environment.GetEnvironmentVariable("IRONWEBSCRAPER_LICENSE_KEY");
C#

IronWebScraper.License.IsLicensed 在指派有效金鑰後返回 true,並且 IronWebScraper.License.IsValidLicense 測試金鑰字串而不應用。 如果沒有上述指派,IronWebScraper 會忽略 IRONWEBSCRAPER_LICENSE_KEY。 它確實能讀取沒有任何代碼的 IronWebScraper.LicenseKey、IronWebScraper_LicenseKey 或 IronSuite.LicenseKey 命名的環境變量。

請注意: IronWebScraper 授權金鑰 也解釋了如何在 Web.config 或 App.config 中設置金鑰。

步驟 3:將您的助手指向 IronWebScraper 的 LLM 文件

  • llms.txt 列出 17 個連結,每個均附一行摘要。
  • llms-full.txt 將 13 頁文件總結成一個文件。
  • llms-full.txt 將 llms-full-catalog.txt 描述為也涵蓋博客文章的索引,但在 2026 年 10 月 17 日,該文件與 llms.txt 相同。
  • API參考列出了每個成員的簽名。
  • 支援技能文件的工具可以使用 skill.md,涵蓋設置、授權、配方和常見錯誤。
  • AI產品基本資料大綱了支援的平台和主要應用案例。

在每次會話開始時提供索引,以及清楚的指令。

Read https://ironsoftware.com/webscraper/csharp/llms.txt before you touch the scraper code again.
Keep to what the pages it links describe.

An IronWebScraper crawl is built like this:
- A scraper class extends WebScraper and overrides Init and Parse.
- Init sets the crawl options and queues the first URL with Request.
- Parse gets a Response, selects nodes with Css or XPath, and queues more pages.
- Scrape appends one JSON line per record to a file under WorkingDirectory.
- Start runs the crawl and returns once the queue is empty.

Response.RequestlUrl has that extra l in the real API, so don't correct it.

For a member those pages don't mention, follow these steps:
1. Look it up at https://ironsoftware.com/webscraper/csharp/object-reference/api/.
2. Compile once to show it exists.
3. If you're still unsure, tell me and don't guess.

Then list every IronWebScraper call here that none of those pages covers.
Text

拼寫 RequestlUrl 的屬性才是真正的成員,而此程序集無 RequestUrl。 輔助工具糾正拼寫後產生的代碼無法編譯。


步驟 4:在項目中添加 IronWebScraper 的環境

在聊天中輸入的指令在會話結束後遺失。 將爬行規則存儲在您的團隊使用的項目文件中。 Claude Code讀取CLAUDE.md,ChatGPT Codex讀取AGENTS.md,GitHub Copilot讀取.github/copilot-instructions.md,Cursor讀取游標/規則/下的.mdc文件或AGENTS.md,Google Antigravity讀取AGENTS.md或GEMINI.md。 單一文本塊與所有相容,雖然游標規則文件還包括MDC前置資料。 從以下內容開始。

Crawlers in this project are written with IronWebScraper and follow these rules:
- The NuGet package and the namespace share one name, `IronWebScraper`.
- The documentation index is https://ironsoftware.com/webscraper/csharp/llms.txt.
- If the tool supports skill files, use https://ironsoftware.com/webscraper/csharp/skill.md too.
- Startup code assigns `IronWebScraper.License.LicenseKey` before any crawl runs.
- Its value is looked up by name in IRONWEBSCRAPER_LICENSE_KEY, and nothing prints the environment.
- The key stays out of console output, log files and commits.
- A scraper is a subclass of `WebScraper` with `Init` and `Parse` overridden.
- No raw `HttpClient` loop fetches pages beside it.
- Pick content out of the `Response` with `Css` or `XPath`, never with regular expressions.
- Add an `HttpIdentity` whose `UserAgent` names the crawler. Without one, no user agent is sent.
- Set `RateLimitPerHost`, `OpenConnectionLimitPerHost` and `HttpRetryAttempts` in `Init`.
- Never lower those limits to speed up a crawl.
- `ObeyRobotsDotTxt` reads robots.txt from the default port only.
- For a site on another port, add its disallowed paths to `BannedUrls`.
- A page that keeps failing never reaches `Parse`. Override `Log` to record which URL failed.
- Each crawl adds lines to the file `Scrape` wrote last time. Delete it before a new crawl.
- `ScrapeUnique` drops the first record it writes to a file.
- Write records with `Scrape` and remove duplicates by id.
- Develop against a local copy of the site before pointing at a live host.
Markdown

技能文件在三個方面已過期。 其單文件範例固定 IronWebScraper 2026.8.1,表示 ObeyRobotsDotTxt 尊重 robots.txt 路徑及速率,並描述 ScrapeUnique 為去重附加。 2026.10.1 的探測矛盾了後兩者。


步驟 5:單次設置提示

步驟1至4可以在專案或會話開始時作為單條消息發送。

Set this project up for IronWebScraper crawls with these rules:
- Keep the IronWebScraper package pinned at 2026.10.1.
- The app fills IronWebScraper.License.LicenseKey before the first crawl.
- It looks up IRONWEBSCRAPER_LICENSE_KEY itself and never enumerates the environment.
- No console line, log or committed file ever contains the key.
- Start from https://ironsoftware.com/webscraper/csharp/llms.txt and keep the code within it.
- Look up anything those pages miss in the API reference that llms.txt links.
- A tool that reads skill files also loads the agent skill listed there.
- Every scraper sets a user agent, a rate limit and a retry count before its first request.
- Nobody removes those settings to make a crawl finish sooner.
- Test selectors on a local copy of a site first.

Save these rules in the file your later sessions load.
Tell me that file's name and list any other change you made.
Text

步驟 6:產生可運行程式碼的提示模式

命名起始頁、待跟隨頁面、欄位和輸出文件。

模糊 使用 IronWebScraper 為活動站點寫個擷取工具。

更好 擴展 WebScraper。 在 Init 中,添加帶命名用戶代理的 HttpIdentity,設置一秒的 RateLimitPerHost,並請求第一個列表頁面。 在 Parse 中,跟隨下一個性,請求每個活動頁面並用第二個解析方法撰寫,每場Scrape到 events.jsonl.

聲明 IronWebScraper 版本和目標框架。

更好 使用 IronWebScraper 2026.10.1 於 .NET 9 中,並於 Start 前指派來自 IronWebScraper_LICENSE_KEY 的金鑰。

請求完整文件。 擷取工具是子類,因此部分片段可省略類宣告或 using IronWebScraper; 指令。

更好 返回整個 Program.cs 及擷取工具類,包含他們的 using 聲明及授權行,準備好即可構建。


步驟 7:驗證您收到的內容

以下每個錯誤在審查生成的代碼時都易被忽視。

  • 既有擷取迴路:用 HttpClient 抓到一頁的請求繞過所有調節設置,因此生成的代碼不應含該類呼叫。
  • 穩定性限值去除以便完成測試:於首次 Request 之前檢查 Init設定。
  • 以屬性開頭的選擇器: Css("[data-event-id]") 在 2026.10.1 中拋出 XPathException,而 Css("article[data-event-id]") 返回了該元素。 在每個屬性測試前加上標籤名稱。

讓代理驗證自己的工作

Rebuild in Release from clean and remove the previous crawl's output files.
Keep events.run1.json, then run the crawl again.

Show me what that run proves:
- Every IronWebScraper class and member used, with its documentation page.
- Whether IronWebScraper.License.IsLicensed came back true.
- The user agent, rate limit, connection limit and retry count set in Init.
- How many records events.json has, plus every event link without one and the reason.

Then tell me whether events.json holds every event the crawl was meant to collect.
Text

IronWebScraper 的爬行默認值

本文探測按預設爬行了在端口 5467 上的本地站點。

設定2026.10.1 的默認設定在測試站點上的效果
ObeyRobotsDotTxttrue沒有 robots.txt 請求抵達站點,且擷取到一個禁止頁面
RateLimitPerHost50 毫秒完整爬行的 42 個請求於 2.55 秒到達
OpenConnectionLimitPerHost2所有 42 個請求通過一個連接發送
HttpRetryAttempts8每個失敗頁面 9 次請求
使用者代理(Identities 為空)none每個請求都不包含 User-Agent 標頭寄出
WorkingDirectory擷取輸出文件出現在當前目錄下的 Scrape 資料夾中

IronWebScraper 從協議和主機名稱構建 robots.txt 地址,並省略端口。 對於測試站點,爬行器嘗試兩個回送地址上的 80 號端口,等待 4.1 秒後連接失敗,然後不採用規則爬行。 其日誌仍報告 robots.txt 將為主機遵守。預設端口上的站點不受影響。 對於任何其他端口上的站點,將不允許的路徑添加到 BannedUrls 中,這樣在探測中會員專享頁被排除在爬行之外。


IronWebScraper 的失敗和跳過頁面

IronWebScraper 處理回應 404 的頁面和回應 500 的頁面是一樣的。

  • HttpRetryAttempts 設為 2 時,每個失敗頁面被請求三次,日誌行依然報告「經過 2 次嘗試」。
  • 設定為 0 時為每個失敗頁面產生一個請求。
  • Parse 不會調用於失敗頁面的任何一次,因此擷取工具從未看到其狀態碼。
  • URL 僅出現在關鍵日誌行「Url 永久失敗」,FailedUrls 計數它。 覆蓋 Log 可以記錄失敗的頁面。
  • 解析方法內拋出的異常效果如同失敗請求一樣。該頁請求了九次,記錄為失敗,爬行繼續。

IronWebScraper 2026.10.1 還跳過了一些在 Init 中請求、但未記錄失敗的頁面。 在這些指南的探測中,爬行器在 Init 抓取三個場館頁面在 26 次執行中的 1 次請求到全部三個。 以此排隊 10 頁的爬行器在 8 次運行中請求 2 至 9 之間的頁面。 未達到站點的跳過頁面,FailedUrls 保持在 0,日誌仍報告「爬行完成」。 在 Init 中請求第一個頁面並從解析方法中排隊其餘頁面,在 8 次運行中 8 次抓取每個場館頁面。


IronWebScraper 的輸出文件

Scrape 將任何物件序列化為一行 JSON 並附加到 JSON 行文件。相對文件名相對於 WorkingDirectory 解決,無文件名的呼叫將寫入以類型命名的文件,如 IronWebScraper.ScrapedData.jsonl。

  • 該文件為不帶字節順序標記的 UTF-8,帶 CRLF 行結尾。 帶重音的標題作為字符本身寫入。
  • DateTimeOffset 值保持其偏移量,且 12.50 的十進價錢保持兩位小數。
  • Scrape 在每次運行時附加。 相同文件中的第二次擷取翻倍該文件,除非程序首先將其刪除。
  • ScrapeUnique 跳過已寫整個 JSON 行的記錄,所以價格變更的事件會再次寫入。
  • ScrapeUnique 也在每個擷取工具實例對每個文件的第一次寫入時丟棄首個記錄。 寫入 A、B、C 和 A 到新文件僅留下 B 和 C。 單次呼叫從未創建文件,完整擷取中首個解析事件遺失於輸出中。

可重現的結果

測試由一個提示組成,您不寫程式碼。 啟動測試站點,在一個空資料夾中新開會話,貼上提示,並將結果與通過標準比較。

請注意: 測試需要 .NET 9 SDK、網絡連接、一個能運行終端命令的助手、IRONWEBSCRAPER_LICENSE_KEY 中的 IronWebScraper 試用或生產金鑰,以及在端口 5467 上的測試站點。gen_site.py 寫出其網頁,serve.py 在回送地址上提供它們並記錄每個請求。

該站點在三個網頁上列出 23 個獨立的活動連結。 二十個指向活動頁面,robots.txt 禁止一個,一個回應404且另一個回應500。有一項活動同時出現在特色框和列表中。

提示

Make a .NET 9 console program that exports a local test site's event listings to JSON.
If this folder already holds project files, say so and stop.
Don't ask me anything.
Write all output here.

Add the NuGet package IronWebScraper 2026.10.1.
Go through https://ironsoftware.com/webscraper/csharp/llms.txt, then its agent skill.
Also read its license keys page and the tutorial /tutorials/webscraping-in-c-sharp/.
Call only members documented there or in the API reference.

The events app's first action sets IronWebScraper.License.LicenseKey.
It reads IRONWEBSCRAPER_LICENSE_KEY by name for that value.
Never let the key reach the console, files, shell echoes or environment variable listings.
If IronWebScraper.License.IsLicensed is false, the app exits with a message.

The site's listings start at http://localhost:5467/events/.
Follow the Next link through every listing page and open each event link.

The IronWebScraper crawler keeps to these limits:
- It requests only http://localhost:5467/ addresses.
- It requests no path that http://localhost:5467/robots.txt disallows.
- It sends the user agent RiversideEventsBot/1.0 on every request.
- It waits at least one second between requests, over one connection at a time.
- It retries a failed page at most twice.

Write a record per loaded event page with IronWebScraper's Scrape or ScrapeUnique.
events.json holds those records as a UTF-8 JSON array sorted by start time.

Each record has these fields:
- id, from the data-event-id attribute.
- title, with its accented letters intact.
- start, the time element's datetime value, unchanged.
- venue.
- price as a number, null when the page says Free.
- tags as an array.
- status in lower case, "scheduled" when the page shows none.
- url, the page's full address.

Also write these files:
- links.json lists every distinct a.event-link target on the listing pages.
- skipped.json gives each failed or disallowed link and its reason.
- requests.log has every URL the crawler requests, with a timestamp.

Each link in links.json ends as a record, a failed request, or a path robots.txt disallows.

Next, do a Release build and run it twice.
Keep the first run's file as events.run1.json and both runs' SHA-256 values.
The second run's events.json must match events.run1.json byte for byte.

Last, add a check mode that sends no requests, and run it to write readback.md.
It reads each actual value from disk.
readback.md gives expected, actual and match for these checks:
- Records plus skipped links equal the distinct event links found.
- No two records share an id.
- Records are in start order, and every start keeps its UTC offset.
- requests.log has no disallowed path, no other host, and no gap under one second.
- The two SHA-256 values are equal.

readback.md names any request you sent the site outside the crawl, and why.

When a check fails, find the cause, fix it and rerun.
Some causes are inside IronWebScraper, with no workaround.
For those, readback.md names the member, what you tried and the unmatched check.
Then finish the rest.

Tell me the Release build result, matching checks out of the total, and your assumptions.
Text

輸出

應用程序的未編輯 readback.md 列出其五個檢查,events.json 和 skipped.json 如應用寫下般可用。 提示在運行后未被編輯。 以下數據來自那些文件和測試網站日誌的獨立讀取。

events.json 中的兩個事件記錄,一個已延期,另一個標題和場地中有重音字母

events.json 第 104 到 129 行由應用撰寫,以純文本繪出。

  • net9.0的Release構建完成,沒有警告也沒有錯誤。
  • events.json 包含 20 條記錄,每個事件頁面各一條,按開始時間排序。
  • 每條記錄的每個欄位皆與其活動頁面相符。 重音的標題和每次開始時間的 UTC 偏移被保留。
  • 擷取工具本身的第一個請求是 robots.txt,並在 AcceptUrl 的覆蓋中拒絕不允許的路徑。
  • 應用程序發送的任何擷取都未請求不允許的路徑。
  • 在應用擁有其 robots.txt 規則之前,會話手動獲取了會員專享頁三次。
  • 兩次 Release 擷取的每次都以用戶代理 RiversideEventsBot/1.0 發送 30 個請求。
  • 這些擷取中請求之間最短間隔為 1.028 秒。
  • 每個失敗頁面收到三次請求,第一次嘗試及兩次重試。
  • 應用程序的注記記錄 ScrapeUnique 丟失了第一個記錄,最終代碼以 Scrape 寫入每個記錄。
  • 在應用的兩次運行之后和單獨一次重建的運行之后,events.json 字節對字節相同。
  • 應用程序的反饋匹配 5 個檢查中的 5 個。
  • 獨立讀書匹配其檢查中的全部 37 個。
  • readback.md 列出發送在兩次Release 擷取之外的 165 個請求,測試站點日誌記錄了 195 個。
  • 專案資料夾中、其Git歷史或Claude Code會話文件中都沒有包含授權密鑰的文件。
  • 設定提示將其規則寫到 112 行的新 CLAUDE.md。
  • 一些步驟與頁面上運行的不同。 步驟 1、3、5 和 7 提示在同次會話中於接受提示後運行。 測試設置阻止了七條有效命令,會話以另一途徑完成每個任務。

什麼過關

  • Release構建完成,無需手動編輯。
  • events.json 按開始時間排序包含 20 條記錄,每個欄位與您自己讀取頁面時的活動頁面相符。
  • skipped.json 命名會員專享頁和兩個失敗的連結。
  • 站點自身的請求日誌顯示無不允許的路徑,無小於一秒間隔的爬行器請求,在失敗頁上最多三次請求。
  • 第二次運行生成相同的 events.json。
  • 專案資料夾中的搜尋找不到包含授權密鑰的文件。
請注意: 若要衡量不同版本,編輯提示的安裝行中的版本并回顧 IronWebScraper 變更日誌 尋找更改。

運行記錄

  • 所有運行使用一台Windows 11 x64桌面,配備Core i9-14900KF和64 GB記憶體。
  • 本指南的專案針對net9.0和固定IronWebScraper 2026.10.1,並且每個擷取都針對在同一機器上提供的測試站點。
  • 指南的持續時間是其頁面上提示的日誌時間之和,而本指南的行只計算接受提示。
  • 表中的版本和型號來自每個工具自己的會話文件和運行日誌,而不是來自單獨版本檢查。
  • 游標 CLI 在其會話文件中不記錄版本,因此其行列提供了覆蓋運行的安裝生成的版本。
  • Last tested 2026年10月26日,用於 Claude Code 指南,其他指導日期在表格中為2026年10月17日。
指南助手和版本模型時長(分鐘)
此指南Claude Code 2.1.277,一次會話,允許提示關閉Claude Opus 523.7
Claude CodeClaude Code 2.1.277,提示 3 在計劃模式聖詩5與Opus 5規劃28.0
ChatGPT CodexCodex CLI 0.160.0GPT-6 Luna27.8
GitHub CopilotCopilot CLI 1.0.95GPT-6 Luna,由Auto為其撰寫18.9
CursorCursor CLI 2026.10.16-e3cb207,headless作曲2.517.0
Google Antigravity反重力CLI 1.4.0,跳過許可提示Gemini 4 Argon (中等)109.9

代理特定指南

以下各指南將此步驟應用於一種工具和一個擷取任務。


疑難排解


問題?

如果您有任何疑問,請聯繫support@ironsoftware.com

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 150,061版本:2026.11剛剛發布