如何在.NET爬虫项目中使用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收到的日志进行比较。 在"Crawling"部分下粘贴块。

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,端口80请求robots.txt,忽略起始URL命名的端口。 在测试博客中它从未请求robots.txt并获取了不允许的草稿。 由于端口80上没有监听,第一个请求在探测进程启动4.5秒后到达博客,将ObeyRobotsDotTxt设置为false将其减少到0.5秒。

第二个探测也在端口80上提供博客的规则。 没有设置用户代理时,Request抛出带有消息"Not a valid user-agent string"的ArgumentException,这些规则中包含Crawl-delay行。有HttpIdentity设置的用户代理时,草稿一直未被请求,请求保持一秒的爬取延迟。 BannedUrls通配符如*/blog/drafts/*在探测中将草稿排除在外。

哪些页面到达Parse,每个被请求多少次?

在默认设置下,探测爬取向测试博客发送了43个请求。 代码两次请求同一解析方法的URL仅被获取一次。 重定向及其目标被视为两个URL,因此重定向的帖子被获取两次,并用同一Parse和FinalUrl到达两次。

回应500的帖子在默认HttpRetryAttempts为8的情况下接收了9个请求,而Parse从未运行。 因此,必须列出其失败项的爬虫将其排队的链接与其写入的行进行比较。

RateLimitPerHost间隔每个请求到主机的开始。在1,000毫秒时,间隙测量为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之前将CodePagesEncodingProvider注册到Encoding.RegisterProvider修复了解码。

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且没有包引用。
  • 快速入门程序为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行。
  • 没有密钥时,程序报告许可失败且未向博客发送请求。
  • 本运行中的演示中没有出现许可证密钥,或会话文件、日志或存储库文件中。
  • 有些步骤与页面中的运行不一样。 会话无头运行,因此键入的回复批准了计划。 运行的守护程序保持代理在存储库内,阻止了四个工具调用,其中两个是错误。

清单的最后八行中保留了旧主题的帖子。

测试运行中content-inventory.csv的标题和最后八行。

测试运行中content-inventory.csv的标题和最后八行,以文本方式从文件中提取。行号为文件中的行号。

Cursor指南还让代理在编写代码之前计划工作,像计划模式在这里一样。 ChatGPT Codex指南在AGENTS.md中保存其爬取规则,Codex在任何工作开始前读取这些规则,就像本指南对CLAUDE.md所做的那样。 Claude Code版本、模型、时机和此次运行的测试机器在共享运行记录中。


故障排除


有问题吗?

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

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

阅读更多

准备开始了吗?

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