如何使用IronWebScraper與Cursor
當C#爬蟲從HttpClient和HtmlAgilityPack轉移到IronWebScraper時,Init中的佇列每個列表頁會遺失記錄。 IronWebScraper 2026.10.1可以在請求所有佇列頁之前結束爬行。 在我們的測試中,四個列表頁在Init中排隊,每個Request呼叫都生成了三個、三個和兩個爬取頁面,三次運行中,輸出沒有錯誤地失去了46個供應商中的10個、10個和24個。
Cursor 是一個基於 VS Code 編碼基礎構建的 AI 程式碼編輯器,具有一個用於編輯文件和運行命令的代理。 本指南使用其兩項功能,文檔鏈接和.commit在.cursor/rules中的專案規則,以遷移採購團隊的供應商目錄爬蟲。 爬蟲從貿易目錄中複製46個供應商及其會員的聯繫信息到CSV文件中,而指南將其移動到IronWebScraper中而不改變CSV中的任何行,且不超過目錄的每秒四次請求上限。
指南遵循高級網頁爬行教程的節流和身份功能。 每次爬行都在localhost上運行的目錄本地副本上進行。在撰寫爬蟲之前,請閱讀公共網站的條款並獲得授權。
快速開始打開HarborDirectory存放庫,將此提示複製並粘貼到新的Agent聊天中。
@src/HarborDirectory/ @README.md
Add a pages command that checks the supplier listing with IronWebScraper.
Invoke it as `dotnet run --project src/HarborDirectory -- pages`.
Stay on IronWebScraper 2026.10.1.
Start with https://ironsoftware.com/webscraper/csharp/llms.txt.
After that, read the advanced tutorial linked from there.
The pages command walks the listing as follows:
- It starts at /directory?page=1 on the test site and follows each Next page link.
- For every listing page, it prints the page number and the number of suppliers on that page.
The directory crawler already loads its key from IRONWEBSCRAPER_LICENSE_KEY in Program.cs.
Don't write the key to any output or file, and don't dump environment variables.
When it builds, run the pages command and show me what it printed.
不要將授權金鑰粘貼到聊天中,因為粘貼的內容可能出現在源文件、提交或構建日誌中。 IronWebScraper 2026.10.1忽略名為IRONWEBSCRAPER_LICENSE_KEY的環境變量,因此存放庫的Program.cs將該變量複製到License.LicenseKey中。 授權金鑰指南列出可以儲存金鑰的位置。
最小工作流程(5 個步驟)
- 從 NuGet 下載 C# 網頁擷取程式庫
- 在.cursor/rules下添加包含IronWebScraper文檔鏈接的專案規則
- 在該規則中記錄網站的請求上限和Init佇列規則
- 在每個請求中參考爬蟲代碼和預期輸出
- 比較每次運行與網站請求日誌,然後在API參考中確認每個成員名稱
您應該使用哪種Cursor模型進行IronWebScraper工作?
Composer是Cursor自己的代理模型。 模型選擇器還提供了Cursor共同開發的SpaceXAI的Grok模型以及來自OpenAI、Anthropic、Google和其他實驗室的模型。 下面的測試運行使用了 Composer 2.5。
您如何將IronWebScraper文檔提供給Cursor?
在請求中包含IronWebScraper的llms.txt鏈接,或者將其存儲在整個團隊共享的規則中。 截至2026年10月,Cursor的@菜單附加文件、文件夾、終端機、聊天、Git差異和瀏覽器,因此IronWebScraper的文檔以鏈接形式到達代理。
高級教程在其示例中設置了RateLimitPerHost,但沒有聲明默認值,且沒有頁面說明兩個爬蟲是否共享一個速率限制。
@src/HarborDirectory/Legacy/PoliteHttp.cs
Answer these five questions about IronWebScraper without editing any files:
1. Which members replace the 500 ms wait that PoliteHttp keeps between requests?
2. What delay and connection limit per host does a new WebScraper use?
3. When a scraper holds several HttpIdentity objects, which one sends a given request?
4. Can an HttpIdentity send a fixed Authorization header, as PoliteHttp does?
5. Do two scrapers running at once against one host share a rate limit?
Answer only using https://ironsoftware.com/webscraper/csharp/llms.txt, plus the pages linked there.
Cite the source page for each answer.
If the docs don't cover a question, write "not documented" and don't guess.
如何編寫IronWebScraper爬蟲代碼的專案規則?
項目規則是存儲在 .cursor/rules 中並與倉庫版本控制的 .mdc 文件。 Cursor 忽略該文件夾中的普通 .md 文件,因為該文件沒有首頁使它無法決定何時加載該規則。
下面的規則在globs中列出了其文件模式,根據Cursor的規則頁面規定用逗號分隔,並將alwaysApply設為false。 然後當某些路徑下的文件處於上下文中時,Cursor會附加此規則。 將其保存為.cursor/rules/harbor-crawlers.mdc。
---
description: IronWebScraper conventions for the Harbor directory crawlers
globs: src/HarborDirectory/**/*.cs, tests/HarborDirectory.Tests/**/*.cs
alwaysApply: false
---
Every scraper in this repository keeps to these rules:
- Fetch and parse pages with IronWebScraper 2026.10.1 only.
- Don't add HttpClient calls or another HTML parser.
- Check IronWebScraper members against https://ironsoftware.com/webscraper/csharp/llms.txt.
- The site at http://localhost:5493 allows 4 requests per second across all scrapers combined.
- Each scraper sets RateLimitPerHost and OpenConnectionLimitPerHost in Init.
- Queue only the first listing page in Init, then follow the Next page link from Parse.
- A login set with NetworkUsername sends each member page twice, once without it and once with it.
- A request that keeps failing never reaches Parse, so check how many records a run wrote.
- When the docs don't list a member, tell me which pages you checked.
關於Init、登錄和失敗請求的規則記錄IronWebScraper在下面部分中測量的行為。
HttpClient和HtmlAgilityPack調用如何映射到IronWebScraper?
舊版爬蟲自行抓取每個頁面,並在請求之間等待。 IronWebScraper將請求排入佇列,根據主機分隔它們,並為每次成功響應調用解析方法。
| HttpClient和HtmlAgilityPack | IronWebScraper |
|---|---|
頁碼循環使用GetAsync | Request於Init中的頁面1,然後從Parse排隊下一頁連結 |
請求之間的Task.Delay | RateLimitPerHost,對於並行請求使用OpenConnectionLimitPerHost |
DefaultRequestHeaders.UserAgent | UserAgent於添加到Identities的HttpIdentity上 |
DefaultRequestHeaders.Authorization 使用基本憑據 | NetworkUsername 和 NetworkPassword 於某身份,或者 HttpIdentity.DefaultHttpRequestHeaders |
| 等候Retry-After於429和503上的重試循環 | HttpRetryAttempts |
SelectNodes 和 SelectSingleNode 借助XPath | Css 或 XPath在Response上,它們返回HtmlNode陣列 |
對於CSV的File.WriteAllTextAsync | Scrape,它將JSON行附加到WorkingDirectory下,或者程式自帶的寫入器 |
為什麼轉移的爬蟲會錯過列表頁?
GitHub Copilot指南衡量IronWebScraper如何丟失在Init中做出的請求。 在本指南的探測中,FailedUrls在丟失頁時保持在0。 接受URL列表的Request過載在三次運行中抓取了四個列表頁中的二個、四個和三個。
Init中的一個請求和從解析方法排隊的每一個進一步請求在8次運行中抓取了所有46個供應商,這就是為什麼規則限制Init到列表的第1頁。
IronWebScraper如何將請求間隔佇於一個主機?
新的WebScraper使用了50毫秒的RateLimitPerHost,2的OpenConnectionLimitPerHost,10的MaxHttpConnectionLimit,8的HttpRetryAttempts和20秒的HttpTimeOut。 借助這些值,爬蟲在50到88毫秒之間發送了50個目錄請求。
對於拒絕太快到達的請求且帶有429和Retry-After標頭的測試站點,默認值失敗。 每個被拒絕的URL請求了9次,間隔相同,Retry-After標頭沒有影響,13個URL被拋棄。 舊爬蟲尊重Retry-After,因此僅依賴重試的移植會丟失記錄。 500毫秒的RateLimitPerHost產生了533到563毫秒的間隙,這使得一個爬蟲在目錄的上限內運行。
如何計劃和執行遷移?
在聊天輸入中按 Shift+Tab 直到選擇了計劃模式,然後發送下面的請求。 Cursor 將計劃寫為一個可編輯的 Markdown 文件。
@src/HarborDirectory/Legacy/ @TestData/expected/suppliers.csv
Plan the move of DirectoryCrawler from HttpClient and HtmlAgilityPack to IronWebScraper.
Leave CertificateScraper out of this change.
Don't write code until I approve the plan.
The plan should answer these questions:
- What replaces the wait, the retry, the user agent and the login in PoliteHttp?
- How does the new scraper reach all four listing pages?
- How does it stay within the request limit in the harbor-crawlers rule?
- How does the suppliers command keep its output file and its column order?
List your assumptions at the end of the plan.
糾正計劃中的任何錯誤。 然後從計劃中構建它,或者切換回Agent並發送此請求。
@harbor-crawlers
The plan is approved. Build it in this order:
1. Run the current suppliers command.
2. Count the rows of out/suppliers.csv that match TestData/expected/suppliers.csv.
3. Replace DirectoryCrawler and PoliteHttp with one IronWebScraper class named SupplierScraper.
4. Remove HtmlAgilityPack from the project file.
5. Build, run the suppliers command again and count the matching rows.
Then tell me both match counts, how long each run took and how many requests the new run sent.
@harbor-crawlers提到按名稱附加規則。
哪個 Cursor 功能適合每個變更?
根據變更的範圍選擇一個特色。 制表符完成開發者已開始的編輯,並且內聯編輯(Ctrl+K)更改可以用一句話描述的選擇。Agent 覆蓋多個文件的更改或需要爬行的工作,顯著錯誤更改比編寫計劃的成本高的情況下,計劃模式值得。此指南中的每個請求都發送到Agent或計劃模式。
哪個身份發送每個請求?
Identities中的每個身份都有自己的用戶代理、cookie和登錄。 沒有身份的情況下,IronWebScraper根本未發出User-Agent標頭。 當有多個身份時,每次請求和每次重試都隨機選擇一個。 持有成員身份和公共身份的爬蟲需要140個請求來獲取46個聯繫頁面,其中94個被拒絕並顯示401。
通過NetworkUsername和NetworkPassword設置的登錄在每個受保護頁面上花費第二個請求。 每個成員頁面首先在沒有憑據的情況下發送,收到401,然後在1.4毫秒內進行有憑據的請求,在RateLimitPerHost間隔之外。 將成員身份傳遞給Request消除隨機選擇但不消除第二個請求。
教程列出了用於自定義標頭的HttpRequestHeaders,但沒有到達網站的標頭放置在該位置。探針添加了Authorization和一個自定義X-Crawler標頭,均未到達。 在創建身份之前添加到靜態HttpIdentity.DefaultHttpRequestHeaders的標頭於每個請求中發送並刪除了每次401。該字典適用於該過程創建的每個身份,包括公共頁面。
如何調試已經使用IronWebScraper的爬蟲?
證書爬蟲在遷移之前已經使用IronWebScraper。 它持有一個公共身份和一個成員身份,在Init中排隊了三個註冊頁面,未設置速率限制。
@src/HarborDirectory/Scrapers/CertificateScraper.cs
The certificates command should write 32 certificates, but it writes far fewer.
The number it writes also varies between runs.
Find the cause before you change anything:
1. Run the command twice and note both counts.
2. Raise the scraper's LoggingLevel and read what happened to the missing certificates.
3. Explain each cause you find.
Then fix CertificateScraper.
Run the command three times and give me the count from each run.
輸出
修復的爬蟲按頁面解析順序將註冊寫為每個證書的一行JSON。

out/certificates.jsonl中的32行的前10行,爬蟲批次運行後以文件中的純文本形式顯示。
當兩個爬蟲抓取同一主機時會發生什麼?
每個WebScraper實例限制其自己的請求。 兩個爬蟲每個將RateLimitPerHost設置為500毫秒並使用一個連接保持兩個請求處於飛行狀態,並向同一主機發送每秒最多6個請求。因此,它們的合併速率必須在代碼中進行規劃。
@src/HarborDirectory/
Add a crawl-all command that runs SupplierScraper and CertificateScraper at the same time.
The command is done when it meets these conditions:
- It writes the same suppliers.csv and certificates.jsonl as the two separate commands.
- Neither scraper gets a 429.
- It prints each scraper's request count and the total run time.
Run crawl-all, compare both files with TestData/expected and tell me what matched.
輸出
該圖表統計本地站點在每秒爬行批次運行中收到的請求。

測試的爬蟲批次運行期間的每秒請求數,從本地站點的請求日誌中提取。100秒內有209次請求。
如何為爬蟲編寫回歸測試?
以其公共構造函數構建的Response從Css拋出ArgumentNullException,ChatGPT Codex指南解釋了為什麼IronWebScraper僅解析其自行獲取的頁面。 因此,測試將保存的頁面通過HTTP服務。
@src/HarborDirectory/ @TestData/pages/
Create an xUnit project at tests/HarborDirectory.Tests and add it to HarborDirectory.slnx.
The tests must not call the Harbor site on port 5493.
Serve the files in TestData/pages from a local listener inside the tests instead.
Write tests for these cases:
- SupplierScraper reads every field from the saved supplier page and contact page.
- CertificateScraper reads every field from the saved certificate page.
- A contact page that answers 401 leaves Email and Phone empty and keeps the supplier.
- A listing page with no Next page link ends the crawl.
Finish by running dotnet test, then report the passing and failing test counts.
在實際運行中,這些提示產生了什麼結果?
每次提交更改代碼的新副版本都在本地站點的新副本下重複所有爬行。腳本閱讀網站的請求日誌並將每個輸出文件與TestData/expected進行比較。
- 移動之前,HttpClient和HtmlAgilityPack爬蟲在96次請求和49.2秒內寫入了所有46個供應商。
- 移動之後,SupplierScraper以相同的標題和行順序寫入相同的46行。
- 新的爬行在57.9秒內發送了142次請求,聯繫頁面對其中46次顯示401。
- Cursor報告新運行的96次請求,僅計算成功的請求。
- SupplierScraper在Init中僅佇列了第一個列表頁,並從Parse中跟隨了下一頁連結。
- Cursor回答了五個文檔問題而未更改文件,未找到說明預設限制或回答雙爬蟲問題的頁面。
- 關於Authorization標頭,Cursor未找到有文檔的示例,且描述HttpRequestHeaders隨每個請求發送,但探測表明這是錯誤的。
- 計劃聲稱在聯繫請求中固定成員身份可以避免雙重獲取,但46次401的回答證明此錯誤。
- 在修正之前,Cursor的運行寫入了1和0張證書,而在獨立運行中寫入了32張中的6張。
- Cursor將故障追溯到隨機身份、隨機池中的成員身份以及缺失的速率限制。
- 即使修正後,CertificateScraper仍從Init中請求所有三個註冊頁,違反專案規則。
- 修正後,命令在三次獨立運行中寫入了32張中的32張證書,每次有67次請求且無429。
- 第一次爬蟲批次構建未收到429,且沒有第二次持有超過4次請求。
- 兩次獨立的爬蟲批次運行在105.2和104.8秒內寫入了所有46個供應商和32張證書。
- 依次運行兩個命令花費了80.8秒。
- xUnit套件通過了4個中的4個測試,在它們運行期間埠5493上的網站沒有記錄請求。
- 一些步驟與頁面上運行的不同。 所有七個提示都通過 Cursor CLI 經過一次無頭會話。 計劃模式在提示03之後持續存在,因此在每次提示05到07中都需要後續消息"構建計劃。"在提示04中,Cursor還更改了頁面爬蟲和README,這不是提示請求的。
GitHub Copilot指南將其IronWebScraper約定限制在路徑特定指令文件中的爬蟲和測試路徑,如此處的規則文件所示。 Google Antigravity指南從相同的高級教程中工作,測量IronWebScraper的網頁緩存和保存的爬蟲狀態保留了哪些內容。運行記錄列出每個指南的助手版本、模型、持續時間和測試機器。
疑難排解
- 高級網頁爬蟲功能 當爬行需要節流或登錄時
- 線程安全性 當一個爬行一次運行多個請求
- 應用授權金鑰 當開始拋出授權異常
- IronWebScraper與JavaScript 當頁面在瀏覽器中已滿但對爬蟲來說是空的時
問題?
如果您有任何疑問,請聯繫support@ironsoftware.com
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。