如何在Cursor中使用IronWebScraper

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

当一个C#爬虫从HttpClient和HtmlAgilityPack迁移到IronWebScraper时,在Init中排队的每个列表页面的端口会丢失记录。 IronWebScraper 2026.10.1可以在请求每个排队页面之前完成这种抓取。 在我们的测试中,四个列表页面在Init中排队,每个页面都有一个Request调用,产生三个、三个和两个获取的页面,结果在三次运行中输出丢失了46个供应商中的10、10和24个,没有出现错误。

Cursor 是一个基于 VS Code 代码库构建的 AI 代码编辑器,带有一个编辑文件并运行命令的代理。 本指南利用两个功能,文档链接和.commit在.cursor/rules中的项目规则,将采购团队的供应商目录爬虫进行迁移。 爬虫将46个供应商及其会员专属联系方式从一个贸易目录复制到一个CSV文件中,指南则在不修改该文件的行或超出目录每秒4个请求限制的情况下将其迁移到IronWebScraper。

指南遵循高级网页抓取教程中的限流和身份功能。 每次抓取都是针对在localhost上提供的目录的本地副本进行的。在为公共网站编写爬虫之前,请务必阅读其条款并获得许可。

快速入门
NuGet使用NuGet安装

PM > Install-Package IronWebScraper

通过在 NuGet 包管理器控制台中运行上述命令安装 IronWebScraper,或在 NuGet 包管理器中搜索该包。

打开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.
Text

不要将许可证密钥粘贴到聊天中,因为粘贴的内容可能会出现在源文件、提交或构建日志中。 IronWebScraper 2026.10.1忽略名为IRONWEBSCRAPER_LICENSE_KEY的环境变量,因此库的Program.cs将该变量复制到License.LicenseKey中。 许可证密钥指南列出了可以存储密钥的位置。



应该为IronWebScraper工作使用哪个Cursor模型?

Composer是Cursor自己的代理模型。 模型选择器还提供Cursor共同开发的SpaceXAI的Grok模型,以及来自OpenAI,Anthropic,Google和其他实验室的模型。 下面的测试运行使用了 Composer 2.5。

如何为Cursor提供IronWebScraper文档?

在请求中包含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.
Text

如何为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.
Markdown

对于Init、登录和失败请求的规则记录IronWebScraper的行为,以下章节将衡量这些行为。

HttpClient和HtmlAgilityPack调用如何映射到IronWebScraper?

旧爬虫自己获取每个页面,并在请求之间等待。 IronWebScraper队列请求,根据主机间隔调用一个解析方法来处理每个成功的响应。

HttpClient和HtmlAgilityPackIronWebScraper
页面号的循环使用GetAsyncRequest用于Init页1,然后Next页面链接从Parse排队
请求之间的Task.DelayRateLimitPerHost,带有OpenConnectionLimitPerHost用于并行请求
在Identities中添加一个HttpIdentity触发的DefaultRequestHeaders.UserAgentUserAgent
使用Basic凭据的DefaultRequestHeaders.AuthorizationNetworkUsername和NetworkPassword在一个身份上,或HttpIdentity.DefaultHttpRequestHeaders
一个等待Retry-After on 429和503的重试循环HttpRetryAttempts
带有XPath的SelectNodes和SelectSingleNode在Response上的Css或XPath,返回HtmlNode数组
对于CSV的File.WriteAllTextAsync这种方式通过Scrape将JSON行附加在WorkingDirectory下,或程序自己的作者

导出的爬虫为什么会错过列表页面?

GitHub Copilot指南衡量了IronWebScraper在Init中丢失请求的情况。 本指南的探测中,丢失页面的FailedUrls保持在0。 从URL列表中提取的Request的重载在三次运行中分别获取了四个列表页面中的两个、四个和三个。

在Init中的一个请求和每个从解析方法排队的进一步请求在8次运行中获取了所有46个供应商,这就是为什么规则将Init限制为列表的第1页。

IronWebScraper如何为一个主机分配请求间隔?

一个新的WebScraper使用了50 ms的RateLimitPerHost,2的OpenConnectionLimitPerHost,10的MaxHttpConnectionLimit,8的HttpRetryAttempts和20秒的HttpTimeOut。 有了这些值,爬虫在50到88毫秒之间发送了50个目录请求。

针对一个拒绝请求到得太快的网站,默认值未能响应429,并且没有依赖Retry-After头。 每个拒绝的URL在相同的间隔请求9次,Retry-After头没有影响,13个URL被放弃。 旧爬虫不仅会遵循Retry-After,因此依赖于重试的端口会丢失记录。 500 ms的RateLimitPerHost产生了533到563 ms的间隙,这使得一个抓取器在目录限制内。

如何规划和进行迁移?

在聊天输入中按 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.
Text

纠正计划中的任何错误。 然后从计划中构建它,或切换回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.
Text

@harbor-crawlers提及通过名称附加规则。

Cursor功能适合哪个变化?

根据更改范围选择一个功能。 Tab完成了开发者已开始的编辑,inline edit(Ctrl+K)更改一个句子可描述的选择。Agent覆盖对多个文件的更改或需要抓取的工作,当不正确的更改撤销成本高于编写计划的成本时,计划模式物有所值。本指南中的每个请求都发送给Agent或计划模式。

哪些身份发送每个请求?

Identities中的每个身份都携带自己的用户代理、cookie和登录。 没有身份时,IronWebScraper完全没有发送User-Agent头。 应对多个身份时,它每次请求和重试随机选择一个。 持有会员身份和公共身份的爬虫需要140次请求才能获取46个联系人页面,其中94个因401被拒绝。

通过NetworkUsername和NetworkPassword设置的登录每个受保护页面需要额外的请求。 每个会员页面首先在没有证书的情况下发出,收到401,随后证书请求在1.4ms内发出,超出了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.
Text

输出

修复版爬虫将注册写入一个按页面解析顺序的JSON行,每份证书对应一行。

out/certificates.jsonl中的前10个32行,每个证书一个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.
Text

输出

该图表统计了本地站点在抓取全过程中每秒收到的请求。

在抓取全过程中,每秒来自SupplierScraper和CertificateScraper的请求的叠堆柱状图,未超过4的限制

在测试的抓取全过程中每秒的请求,从本地网站的请求日志中提取。209个请求在100秒内。

如何为抓取器编写回归测试?

用其公开构造函数构建的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.
Text

在实际运行中这些提示生成了什么?

每次提交更改代码的新副本重复了下面的每次抓取,对新的本地站点副本执行。一个脚本读取站点的请求日志,并将每个输出文件与TestData/expected进行比较。

  • 迁移前,HttpClient和HtmlAgilityPack爬虫在96次请求和49.2秒内写出了全部46个供应商。
  • 迁移后,SupplierScraper写出了相同的46行,头部和行序相同。
  • 新抓取在57.9秒内发送了142个请求,联系页面回答了其中的46个错误401。
  • Cursor报告了新运行中96个请求,仅成功请求数。
  • SupplierScraper仅将第一个列表页面排队在Init中,并从Parse中跟随Next页面链接。
  • Cursor在未更改文件的情况下回答了五个文档问题,并未找到声明默认限制或回答两个抓取器问题的页面。
  • 对于Authorization头,Cursor未找到记录的示例并描述HttpRequestHeaders随每个请求发送,探测过程表明这是不正确的。
  • 计划声称将成员身份固定在联系请求上避免了双重获取,而46个401的响应证明这是不对的。
  • 修复前,Cursor的运行中证书命令写出了1和0的证书,独立运行中6个32。
  • 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的网络缓存和已保存抓取状态。运行记录列出了每个指南的助理版本、模型、持续时间和测试机器。


故障排除


有问题吗?

如果您有任何问题,请联系support@ironsoftware.com

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 150,061版本:2026.11刚刚发布