如何使用IronWebScraper與Claude Code

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

一個爬蟲很容易撰寫,但也很容易寫得很差。 劣質版本僅在您面前的頁面上運行,忽略了其指向的主機,並在佈局首次更改時停止正確運行。 計劃將公司博客搬遷到新平台的行銷團隊需要首先取得每篇已發表文章的清單,其中包含每篇文章的地址、標題、作者、發佈日期、分類、標籤、字數和圖片。 檔案庫涵蓋了五年和兩個網站主題,其中一篇文章沒有任何歸檔頁面的連結。 Anthropic構建了Claude Code作為一個程式編寫代理,可從命令行內、VS Code或JetBrains中使用,也可作為桌面應用程式和在瀏覽器中使用。 在所有這些情況下,它能讀取程式碼、編輯文件和運行命令,這適合爬蟲的工作,其中每次選擇器的更改都伴隨著再次運行和查看輸出的步驟。

IronWebScraper是圍繞一個基礎類構建的。 您需要擴展WebScraper,並重寫Init以請求起始URL和設定爬取規則,然後重寫Parse以從每個回應中提取值。 隊列和節流已經就緒,因此代理只需撰寫依賴於網站的部分。博客教程顯示了在博客上的使用範例。 本指南解釋了如何配置Claude Code以生成內容清單,使用四個經測試的提示,並記錄每個提示所依賴的IronWebScraper行為。

Anthropic將Claude Fable 5.1和Claude Fable 5作為Claude Code中最有能力的模型,適合於大型的任務。 /model 命令更改模型而不結束會話。

在每個提示中命名字段、輸出文件和爬取限制,然後將其餘部分留給代理完成。

快速開始
NuGet使用NuGet安裝

PM > Install-Package IronWebScraper

在 NuGet 包管理器控制台中運行上述命令安裝 IronWebScraper,或在 NuGet 包管理器中搜索該包。

在專案資料夾中打開終端機,運行claude,然後在第一個提示之前輸入/model opusplan。 測試在本地機器的5437埠上提供了公司博客的副本,每個提示都命名該地址。

Add the IronWebScraper 2026.10.1 package to BlogInventory.csproj.
Then make Program.cs a smoke test for the company blog at http://localhost:5437/blog/.

The inventory tool puts the value of IRONWEBSCRAPER_LICENSE_KEY in License.LicenseKey first.
Keep that key private with these rules:

- The key never goes to the console, a log or a file.
- Don't run env, printenv, Get-ChildItem Env: or any other command that lists the environment.
- When you check the variable, answer yes or no to whether it has a value.

After setting the key, Program.cs does these things in order:

1. Prints License.IsLicensed.
2. Runs a WebScraper subclass that requests http://localhost:5437/blog/ and no other page.
3. Prints how many post cards that page lists, and the title of the first card.

Run it once and show me the console output.
Text


Claude Code在.NET爬蟲專案中做了什麼?

代理撰寫了WebScraper 子類,並在測試博客上運行。 開發人員決定爬取哪個網站、速度如何以及排除哪些頁面。

下一節中的專案規則禁止手寫請求循環,並命名封裝及其版本,因此代理不會僅根據名稱選擇封裝。

您如何設置Claude Code?

按照Anthropic的快速入門安裝Claude Code並登入。 當claude 在終端機中啟動並響應時,設置即完成。

如何設立一個Claude Code可以工作的項目?

首先給代理一個真實的目標。測試夾具是一個沒有封裝引用的.NET 9控制台應用程式,博客作為一個本地主機運行,擁有26篇文章。 從2023年起的文章使用當前主題,而較早的九篇文章使用先前的主題。 檔案庫透過重定向連結了一篇文章,一篇文章回答服務器錯誤,一篇較舊的文章採用windows-1252編碼發送,而robots.txt禁止一個草稿夾,第二個檔案庫頁面連結到該夾。

CLAUDE.md在每個會話開始時加載,因此那裡寫的規則適用於每個後續的提示。 第二個提示創建文件及一個僅讀的爬蟲程式代碼審閱員。

Create CLAUDE.md for this repo, with a Crawling section that later sessions follow:

- IronWebScraper fetches and parses every page. No HttpClient, HtmlAgilityPack or AngleSharp.
- Stay on the IronWebScraper version that BlogInventory.csproj has now.
- The crawler only ever requests http://localhost:5437.
- Check each IronWebScraper member against docs/IronWebScraper.xml before using it.
- Crawl results go under out/.

Copy IronWebScraper.xml into docs/ now.
Your NuGet global packages folder has it in ironwebscraper/2026.10.1/lib/netstandard2.1.

Next, create a subagent at .claude/agents/crawl-reviewer.md.
Give it only the Read, Grep and Glob tools.
It reviews Inventory/ and answers four questions:

- Which IronWebScraper members does the code call, and which are missing from the XML?
- Does anything fetch a page without going through IronWebScraper?
- Which robots.txt rules in CLAUDE.md does the crawler skip?
- Which output calls does CLAUDE.md warn against?

I'm adding IronWebScraper's measured crawl rules to CLAUDE.md before the first review.
Tell me the path you copied the XML from.
Text

除了成員清單外,IronWebScraper在llms.txt中列出其代理文件。

哪個IronWebScraper爬蝕規則屬於CLAUDE.md?

每行來自一個探頭,無論是否使用授權密鑰均爬取本地博客,並將網站的請求日誌與Parse收訖的日誌進行比較。 在爬取部分下貼上區塊。

These crawl rules were measured on IronWebScraper 2026.10.1:

- IronWebScraper never reads IRONWEBSCRAPER_LICENSE_KEY on its own.
- Assign License.LicenseKey from that variable before Start.
- With no valid key, Start throws LicensingException before the first request.
- LicensingException is not accessible, so catching it by name fails with CS0122.
- Compare the exception's type name as a string instead.
- Requests carry no User-Agent header until an HttpIdentity sets one.
- IronWebScraper reads robots.txt from port 80 of the host, whatever port the start URL uses.
- On localhost:5437 no rule applies, and the failed lookup delays the first request by 4 s.
- Set ObeyRobotsDotTxt to false.
- In Init, read http://localhost:5437/robots.txt with FetchUrlContents.
- Turn each Disallow path into a BannedUrls glob, and Crawl-delay into RateLimitPerHost.
- RateLimitPerHost spaces the starts of requests to one host. Its default is 50 ms.
- OpenConnectionLimitPerHost defaults to 2, so a slow page overlaps the next request.
- A second Request for a URL already seen is dropped when its parse method matches.
- A different parse method fetches it again. A redirect and its target are two URLs.
- Two Parse calls can then share one Response.FinalUrl, so deduplicate on FinalUrl.
- By default a URL that returns 500 gets 9 requests, because HttpRetryAttempts is 8.
- Parse never sees that page, and FailedUrls counts it.
- Call Start() with no CrawlId. After a finished crawl, Start(CrawlId) parses nothing.
- Scrape appends to its file on every run, so delete the old file first.
- ScrapeUnique drops the first record each scraper writes to a file, even an existing file.
- Don't use ScrapeUnique for the inventory.
- A windows-1252 page is decoded as UTF-8, though Response.CharSet says windows-1252.
- Call Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) before Start to fix that.
Markdown

IronWebScraper如何應用robots.txt到站點的本地副本?

ObeyRobotsDotTxt 預設打開,但是IronWebScraper從http://localhost/robots.txt,的port 80請求robots.txt,而不管起始URL指定了哪個端口。 在測試博客上從未請求robots.txt並獲取了被禁止的草稿。 由於port 80無人在聽,探頭程序啟動後4.5秒首次請求抵達博客,將ObeyRobotsDotTxt設為false則降到0.5秒。

第二個探頭也在port 80上提供博客規則。 在未設置用戶代理的情況下,Request拋出ArgumentException,消息為“Not a valid user-agent string”,這裡的規則包括Crawl-delay一行。設置了用戶代理的HttpIdentity時,草稿保持未請求狀態,請求也保持了一秒的爬行延遲。 一個如BannedUrls的glob(例如*/blog/drafts/*)在探頭中將草稿排除在外。

哪些頁面到達Parse,每個頁面請求了多少次?

使用默認設置,探頭爬行向測試博客發送了43個請求。 一個用相同解析方法請求兩次的URL僅獲取了一次。 重定向及其目標計為兩個URL,因此重定向的文章被請求了兩次,以相同的Parse到達FinalUrl。

回應為500的文章在默認HttpRetryAttempts為8的情況下,收到了9個請求,Parse從未為它運行。 必須列出失敗的爬蟲因此會將排隊的鏈接與其撰寫的行進行比較。

RateLimitPerHost 為每次向一個主機的請求開頭留下空隙。在1000毫秒時,間隙測量為1.03到1.06秒,但回應需時2.5秒的文章重疊了兩個更晚的請求,因為OpenConnectionLimitPerHost預設為2。因此,IronWebScraper也按非頁面順序發送請求,因此需要有序輸出的爬蟲在寫行之前會進行排序。

IronWebScraper會向輸出文件寫入什麼?

Scrape 添加到其JSON Lines文件中,並將同一工作文件夾中的第二次運行將探頭文件從27行增加到54行。ScrapeUnique 丟掉每個爬蟲寫入文件的第一條記錄,而在所有14次探頭爬行中唯一檔案中,第一篇文章遺失。

windows-1252的文章失去了它的重音。 Response.CharSet 報告windows-1252,然而文本被解碼為UTF-8,作者的名字到達時,代替重音字母的是替換字符。 來自同一作者的以UTF-8傳送的文章正確解碼。 在Start之前通過Encoding.RegisterProvider 註冊CodePagesEncodingProvider修復了解碼。

在瀏覽器中,windows-1252的文章看起來像這樣。 清單必須保持作者名字中的重音字母。

windows-1252的文章在本地測試博客上,擁有一個帶重音的作者名字

在Chrome顯示的本地測試博客上的windows-1252文章。該站點及其文章是為測試而建立的。

如何提示IronWebScraper代碼編譯?

在一個提示中請求爬蟲及其輸出規則,並允許計畫模式定下設計,然後才對文件做出改變。 在發送提示前,按Shift+Tab切換到計畫模式。 在選擇opusplan時,Opus寫計劃,Sonnet在計劃被批准後執行。

Build the inventory crawler in Inventory/BlogInventoryScraper.cs, and have Program.cs run it.
It starts at http://localhost:5437/blog/ and crawls by these rules:

- Follow the Older posts links and every category page.
- One post is on no archive page, but its category page links to it.
- Apply the site's robots.txt the way CLAUDE.md describes.
- Send LarkspurInventory/1.0 as the user agent.
- Give up on a failing page after two retries.
- Posts from 2021 and 2022 use the old theme, so read both layouts.

Write out/content-inventory.csv with one row per post, in this shape:

- The columns are Url, Title, Author, Published, Layout, Categories, Tags, Words and Images.
- Url is where the post was served from, after any redirect.
- Published is yyyy-MM-dd, and Layout is current or old.
- Categories and Tags are joined with semicolons. Old-theme posts have no tags.
- Words is the number of whitespace-separated words in the body.
- Images counts the img tags in the body.
- Rows go newest first, and each run replaces the file.

Put every post URL that answered with an error in out/failed-pages.txt, one URL per line.

Run it, then tell me the row count and which pages failed.
After that, have the crawl-reviewer subagent review Inventory/ and give me its findings.
Text

提示的兩部分保護清單。

  1. 每個服務地址一行:清單供遷移使用,而在兩個地址下列出的文章會被移動兩次。
  2. 單獨文件中的故障: 沒有它清單中遺漏的文章和從未發佈的文章無法區分。
提示: 僅當robots.txt規則成為BannedUrls和RateLimitPerHost設置而且沒有行被ScrapeUnique寫入時批准計劃。

當博客改變其文章模板時會發生什麼?

測試在第三和第四個提示之間將本地博客切換到新文章模板。檔案頁面和較舊的文章保留它們的標記,而每篇來自2023年起的文章都移動到新類名。 以@開頭的路徑將該文件附加到請求。

The blog moved its posts to a new template this morning.
Check the crawler against it with these steps:

1. Copy @out/content-inventory.csv to before-template.csv in the repo root, and keep it unchanged.
2. Run the crawler again and compare the new inventory with the copy.
3. Fix the crawler so it reads the old theme, the current theme and the new template.
4. Repeat steps 2 and 3 until every row matches before-template.csv, at most three times.

Posts in the new template still get Layout current.
Tell me which fields the new template broke, what you changed, and anything that still differs.
Text

在相信生成的爬蟲代碼之前應該驗證什麼?

在審閱中包括以下檢查。

  • 請求日誌:計算每頁請求,確認是否獲取了不允許的路徑下的內容,並測量請求之間的間隙。
  • 構建:dotnet build 拒絕不存在的成員,如Response.RequestlUrl的更正拼寫。
  • 幾行的樣本:開啟每個主題的幾篇文章,並在遷移開始之前將它們的字段與清單中的比較。

要獲取具有固定通過標準並可重新運行的完整IronWebScraper任務,請參閱AI編碼助手指南。

在實際運行中,這些提示產生了什麼結果?

Claude Code在一個會話中按順序運行四個提示,對著本地博客。 規則塊在第三個提示前進入CLAUDE.md。 沒有代碼是手工編輯的。

  • 運行之前,倉庫中只有一行Program.cs且沒有封裝引用。
  • Quickstart程序在IsLicensed返回True,計算了10個文章卡片並正確命名第一篇文章。
  • CLAUDE.md 收到五個爬取規則,子代理文件僅列出Read、Grep和Glob工具。
  • 在計劃期間,會話及其子代理向博客發送了370個curl請求,其中5個到被禁止的草稿夾。
  • 爬蟲設定了ObeyRobotsDotTxt為false,自己讀取robots.txt並將其規則轉換為BannedUrls和RateLimitPerHost設置。
  • 第三個提示的爬行發送了38個請求,全用LarkspurInventory/1.0作為用戶代理,沒有一個到草稿夾。
  • 請求開始之間的中位時間間隙為1.04秒,兩個較短的間隙位於robots.txt和一個重定向之後。
  • 在該次爬行中,失敗的文章收到了3個請求,並且是failed-pages.txt中唯一的條目。
  • 在第三個提示之後,清單擁有25行,每個字段與我們檢查中博客自己的數據匹配。
  • 重定向的文章出現在一次下,使用其最終地址,缺失於檔案頁面的文章存在。
  • windows-1252的文章保留其帶重音的作者名,沒有行包含替換字符。
  • 在新模板上,第三個提示的爬蟲寫了8行並列出了19個失敗地址,儘管只有一次文章單獨返回錯誤。
  • 在第四個提示中一個修補後,新清單與更改模板前的拷貝相同。
  • 最終代碼的新複製品在當前主題和新模板運行下都產生相同的25行。
  • 沒有密鑰時,程序報告授權失敗,未向博客發送請求。
  • 授權密鑰未在此次運行中出現在任何筆錄、會話文件、日誌或倉庫文件中。
  • 一些步驟與頁面上運行的不同。 會話無頭運行,所以鍵入的回覆批准計劃。 運行的守護程序保持代理在倉庫內,並阻止了四個工具調用,其中兩個是錯誤的。

清單的最後八行保存了舊主題的文章。

從測試運行內容清單的表頭和最后八行。

從測試運行中,從檔案中提取的文本繪製的內容清單的表頭和最後八行。行號為文件所屬。

Cursor指南 也讓代理在撰寫代碼之前計畫工作,如此處的計畫模式所做。 ChatGPT Codex指南 將其爬行規則保持在AGENTS.md中,Codex在開始工作之前讀取規則,如此指南中使用CLAUDE.md。 此運行的Claude Code版本、模型、時間和測試機器在共享的運行記錄中。


疑難排解


問題?

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

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

閱讀更多

準備好開始了嗎?

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