使用IronWebScraper与AI编码助手
IronWebScraper是Iron Software的适用于.NET的网页抓取库。 它在C#中爬取网站,跟随页面间的链接,并从HTML中提取结构化数据,一个包涵盖所有运行.NET Standard 2.0的平台。
编码助手可以从单个请求生成.NET网页抓取器。无论该代码是否尊重其目标网站,以及是否能继续在第二页工作,都需要进行检查。 一个从记忆中工作的助手可以编写一个原始的HttpClient循环,添加自己的重试逻辑,并在连接允许的情况下最快发送请求。 IronWebScraper已经管理了请求队列、节流和身份,因此助手需要编写的代码是具有两个覆盖的一个类。 在本指南的测试运行中,使用可用的文档助手编写了一个WebScraper子类,并没有调用HttpClient。
每位助手的设置过程都是一样的。 添加包,从环境中分配密钥,提供IronWebScraper的机器可读文档给助手,并在标记任务完成之前将爬虫的输出及其请求与网站进行比较。 这适用于基本抓取教程、博客抓取、购物网站抓取和高级设置。
如何使用IronWebScraper与AI编码助手
- 从NuGet下载C#网页抓取库
- 从启动时的
IronWebScraper_LICENSE_KEY分配IronWebScraper.License.LicenseKey - 在助手编写代码之前给予IronWebScraper的llms.txt索引
- 在您的助手加载的指导文件中记录爬取规则
- 在第一个请求之前设置用户代理、速率限制和重试次数
- 比较输出文件与网站的请求日志和页面
我的AI助手不能自己编写抓取器吗?
可以编写一个。 困难出现在程序外,在目标网站接收到的内容和输出文件持有的内容中。
- 运行的抓取器仍然可以超载网站:
RateLimitPerHost设置对一个主机的请求之间的最小延迟,而OpenConnectionLimitPerHost限制并行连接数。 - HTML选择不需要正则表达式:
Response提供Css、XPath、QuerySelector和GetElementById。 - 精确命名包:包和命名空间都是
IronWebScraper。 攻击者注册助手可能误产生的包名,这种做法称为slopsquatting。 - 许可条款预先定义:许可页面列出了每个层级允许的开发者人数。
第一步:安装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.
从终端显式固定版本发布。
dotnet add package IronWebScraper --version 2026.10.1
该包不包含浏览器引擎和本机二进制文件。 其唯一依赖项是IronSoftware.Common和IronSoftware.System.Drawing,第二个带来了SixLabors.ImageSharp 3.1.12。对于该版本,dotnet list package --vulnerable --include-transitive报告了五个建议,其中三个评级为高。
步骤 2:应用您的许可证密钥
没有密钥,Start在爬虫发送单个请求之前抛出IronSoftware.Exceptions.LicensingException,并且没有任何内容到达输出文件夹。 消息提到免费开发使用七天,但在对此文章的探测的第一次调用中例外出现。 键入错误的密钥会导致“无法识别”的消息,该消息未重复密钥。 下面提供了一个免费试用密钥。
从今天开始在您的项目中使用 IronWebScraper 免费试用。
在应用启动时分配一次许可证密钥,在第一次爬取之前。 从环境变量中检索它,因此不会将其存储在存储库中。
IronWebScraper.License.LicenseKey = Environment.GetEnvironmentVariable("IRONWEBSCRAPER_LICENSE_KEY");
分配有效密钥后,IronWebScraper.License.IsLicensed返回true,IronWebScraper.License.IsValidLicense测试密钥字符串而不应用它。 如果没有上面的分配,IronWebScraper将忽略IRONWEBSCRAPER_LICENSE_KEY。 它确实读取名为IronWebScraper.LicenseKey、IronWebScraper_LicenseKey或IronSuite.LicenseKey的环境变量,没需要任何代码。
步骤 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.
拼写为RequestlUrl的属性是真实成员,程序集中没有RequestUrl。 纠正拼写的助手生成的代码无法编译。
步骤 4:在您的项目中添加 IronWebScraper 上下文
在聊天中输入的指示会话结束后会丢失。 将爬取规则存储在您的团队使用的项目文件中。 Claude Code 读取 CLAUDE.md,ChatGPT Codex 读取 AGENTS.md,GitHub Copilot 读取 .github/copilot-instructions.md,Cursor 在 .cursor/rules/或 AGENTS.md 中读取 .mdc 文件,Google Antigravity 读取 AGENTS.md 或 GEMINI.md。 单个文本块与所有人兼容,虽然 Cursor 规则文件也包括 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.
技能文件在三个方面过时。 其单文件示例固定于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.
步骤6:产生可用代码的提示模式
命名开始页面、要跟随的页面、字段和输出文件。
不明确 使用IronWebScraper为一个事件网站编写爬取器。
更好 扩展
WebScraper。 在Init中,添加一个具有已命名用户代理的HttpIdentity,设置一秒的RateLimitPerHost,并请求第一个列表页面。 在Parse中,跟随下一页链接,请求每个事件页面使用第二个解析方法,并使用Scrape将每个事件记录写入events.jsonl.
说明IronWebScraper版本和目标框架。
更好 在.NET 9上使用IronWebScraper 2026.10.1,并在
Start之前从IronWebScraper_LICENSE_KEY分配密钥。
请求完整文件。 爬取器是一个子类,因此部分代码片段可以省略类声明或using IronWebScraper;指令。
更好 返回完整的Program.cs和爬取器类,带有其using指令和许可证行,准备好按原样构建。
步骤7:验证收到的内容
以下每个错误在生成代码的审核中很容易被忽略。
- 库旁边的请求循环:使用
HttpClient抓取的页面绕过每个节流设置,因此生成的代码中不应包含此类调用。 - 为了使测试完成而删除的速率限制:检查
Init中的设置,在第一个Request之前。 - 以属性开头的选择器:
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.
使用IronWebScraper的默认爬取
本文的探测在端口5467上抓取了一个本地站点,将每个默认值保留在原位。
| 设置 | 2026.10.1中的默认值 | 在测试网站上测量的效果 |
|---|---|---|
ObeyRobotsDotTxt | true | 没有robots.txt请求到达站点,并且抓取了不允许的页面 |
RateLimitPerHost | 50毫秒 | 完整抓取的42个请求在2.55秒内到达 |
OpenConnectionLimitPerHost | 2 | 所有42个请求通过一个连接到达 |
HttpRetryAttempts | 8 | 每个失败页面的九个请求 |
用户代理(Identities为空) | none | 每个请求都在没有User-Agent标题的情况下发送 |
WorkingDirectory | 抓取 | 输出文件出现在当前目录下的抓取文件夹中 |
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次运行中每次都获取了每个场地页面。
使用IronWebScraper的输出文件
Scrape将任何对象序列化为一行JSON并将其附加到JSON Lines文件。相对文件名相对于WorkingDirectory解析,未指定文件名的调用写入一个以类型命名的文件,例如IronWebScraper.ScrapedData.jsonl。
- 文件为不带字节顺序标记的UTF-8格式,行结束为CRLF。 带重音的标题以字符本身写入。
DateTimeOffset值保持其偏移,12.50的十进制价格保持了两个数字。Scrape在每次运行中添加。 在同一文件夹中进行第二次爬取会使文件加倍,除非程序先删除它。ScrapeUnique跳过整个JSON行已写入的记录,因此相同事件价格更改时会再次写入。ScrapeUnique还丢弃了每个抓取器实例中每个文件写入的第一条记录。 将A、B、C和A写入新文件,留下B和C。 单次调用从未创建文件,在完整抓取中解析的第一个事件在输出中缺失。
一个您可以重复的结果
测试包括一个提示,您不必编写其中的任何代码。 服务测试站点,在空文件夹中打开新会话,粘贴提示,并与通行标准结果进行比较。
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.
输出
应用未编辑的readback.md列出了其五个检查项,和events.json及skipped.json可用,就像应用写入它们时那样。 运行后,提示未被编辑。 以下数字来自对这些文件及测试站点日志的独立阅读。

events.json的第104到129行如应用所写,以纯文本绘制。
- 适用于 net9.0 的发布版本完成,没有警告和错误。
- events.json中持有20条记录,每个事件页面一条,按开始时间排序。
- 每个记录的每个字段都匹配其事件页面。 保留了每个开始时间的重音标题和UTC偏移。
- 爬虫本身请求robots.txt为首个请求,并在AcceptUrl的覆盖中拒绝不允许的路径。
- 应用程序发送的爬取没有请求不允许的路径。
- 在应用程序有robots.txt规则之前,会话手动抓取了仅限会员页面三次。
- 每个两个发布爬取请求了30次请求,用户代理是RiversideEventsBot/1.0。
- 在这些爬取中请求之间的最短间隔是1.028秒。
- 每个失败页面请求了三次,第一次尝试和两次重试。
- 应用程序的说明记录到ScrapeUnique丢失了第一条记录,最终代码使用Scrape写入了每条记录。
- 应用程序的两次运行后,events.json字节相同,更别说两个单独重建的运行。
- 应用程序的反馈符合 5 项检查中的 5 项。
- 独立阅读匹配了其所有37项检查。
- readback.md列出了发送的165个请求,两个发布爬取之外的,测试站点的日志记录了195个。
- 项目文件夹中、其 Git 历史或 Claude Code 会话文件中没有包含许可证密钥的文件。
- 设置提示将其规则写入新的CLAUDE.md,共112行。
- 有些步骤与页面中的运行不一样。 第一个、第三个、第五个和第七个步骤提示在同意提示后的同一会话中运行。 测试设置阻止了七个有效命令,会话通过其他方式完成了各个任务。
通过的标准
- 释放构建完成,无需手动编辑。
- events.json持有按开始时间排序的20个记录,每个字段在您自己读取页面时匹配其事件页面。
- skipped.json命名了仅限会员页面和失败的两个链接。
- 网站自己的请求日志显示没有不允许的路径,爬虫请求之间没有小于一秒的间隔,且最多三个请求用于失败页面。
- 第二次运行生成相同的events.json。
- 搜索项目文件夹找不到包含许可证密钥的文件。
运行记录
- 所有运行都使用一台 Windows 11 x64 台式机,搭载 Core i9-14900KF 和 64 GB 内存。
- 本指南的项目目标是net9.0,并将
IronWebScraper定为2026.10.1,每个爬取针对在同一台机器上提供的测试站点。 - 指南的持续时间是其页面上提示的日志时间总和,并且此指南的行数仅计算接受提示的。
- 表中的版本和型号来自每个工具自己的会话文件和运行日志,而不是独立的版本检查。
- Cursor CLI在其会话文件中未记录版本,因此其行给出了安装的构建,其处理记录涵盖了运行。
- 最后测试于 2026年10月26日,适用于Claude Code指南,2026年10月17日适用于表中每个其他指南。
| 指南 | 助手和版本 | 模型 | 持续时间(分钟) |
|---|---|---|---|
| 本指南 | Claude Code 2.1.277,一个会话,关闭权限提示 | Claude Opus 5 | 23.7 |
| Claude Code | Claude Code 2.1.277,计划模式下的提示3 | 与 Opus 5 计划的第 5个奏鸣曲 | 28.0 |
| ChatGPT Codex | Codex CLI 0.160.0 | GPT-6 Luna | 27.8 |
| GitHub Copilot | Copilot CLI 1.0.95 | GPT-6 Luna,Auto 选择 | 18.9 |
| Cursor | Cursor CLI 2026.10.16-e3cb207,无头模式 | Composer 2.5 | 17.0 |
| Google Antigravity | Antigravity CLI 1.4.0,跳过权限提示 | Gemini 4 Argon (Medium) | 109.9 |
代理特定指南
下列每个指南将这些步骤应用于一个工具和一个抓取任务。
- 使用IronWebScraper与Claude Code将公司博客档案转为内容清单
- 使用IronWebScraper与ChatGPT Codex进行分页产品目录的价格监控
- 使用IronWebScraper与GitHub Copilot进行影院节目时间聚合器
- 使用IronWebScraper与Cursor将供应商目录爬虫从HttpClient和HtmlAgilityPack转移开
- 使用IronWebScraper与Google Antigravity实现一个在停止后恢复的工作板爬虫
故障排除
- IronWebScraper许可证密钥如果
Start抛出许可证异常 - IronWebScraper和JavaScript如果页面显示为空,因为其内容在加载后到达
- 线程安全如果并行解析产生的结果不相加
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。