如何使用IronWebScraper与Google Antigravity
一个招聘分析团队每天早晨从一个区域招聘网站收集每个列表。 当服务器重启、笔记本电脑进入睡眠状态或有人按下Ctrl+C时,几百页的爬取可能会中途停止。 当爬虫再次启动时,它必须完成剩余的页面,而不是重复下载已完成的页面,也不得重复写入任务。爬取也可能在没有错误的情况下结束,但仍然记录页面不再显示的工资。 这两个错误都不会在控制台日志中出现。
Google Antigravity可以像工作搜索者查看页面一样检查爬取。 其/browser命令启动一个浏览器子代理,会打开每个页面并捕获截图。 IronWebScraper保留一个网页缓存并可以在爬取ID下重新启动爬取,这些功能在其高级网页抓取功能教程中有所涉及。 本指南使用这两个工具创建了一个招聘网站爬虫,并对一个已知60个列表的本地副本进行测试。
本指南适用于使用AI代理编写.NET爬虫的开发人员。 其中每次爬取都针对在localhost上提供的测试版块,并且不涉及爬取您未管理的网站。
快速入门在Antigravity中打开HarborJobs.Crawler项目,描述爬取。
/plan Build a crawler for Harbor Jobs, the local test board at http://localhost:5481.
This repo is a .NET 9 console app named HarborJobs.Crawler.
Install IronWebScraper 2026.10.1 and subclass WebScraper.
The crawl runs in this order:
1. Start at /jobs/page-1.html and follow the rel="next" links through page 6.
2. Open every job linked from a listing page.
3. Write one line per job to output/jobs.jsonl.
A line holds the job id, title, company, location, salary, posted date and a closed flag.
The crawler also follows these rules:
- It requests nothing outside localhost:5481, and at most one page every 250 ms.
- It obeys robots.txt, and its user agent is HarborJobsCrawler/1.0.
- IronWebScraper's working directory is output/.
- Pages go into IronWebScraper's web cache, so a restart doesn't download them again.
- The crawl gets an id, so a stopped run can start again under that id.
- A --stop-after N option stops the crawl after N jobs, for testing restarts.
Program.cs reads IRONWEBSCRAPER_LICENSE_KEY and sets IronWebScraper.License.LicenseKey once.
The key stays out of output, logs and files. Don't list environment variables either.
The Implementation Plan should answer two questions:
- Where on disk do the cache and the saved crawl state go?
- What happens to queued requests when a crawl stops?
Once the plan is done, build the crawler and run one full crawl.
Tell me how many lines output/jobs.jsonl ends up with.
IronWebScraper本身不读取IRONWEBSCRAPER_LICENSE_KEY,因此提示让Program.cs从中分配License.LicenseKey。 许可证密钥指南描述了应用密钥的其他方法。
最小工作流程(5 个步骤)
- 从NuGet下载C#网页抓取库
- 在build的控制台项目上启动代理,测试站点位于localhost
- 为爬取提供一个工作目录、一份网页缓存以及停止执行N个任务的方法
- 让/browser比较工作页面与输出文件
- 停止并重新启动爬取,然后确认每个API参考文档中的成员
什么是Google Antigravity,它与聊天助手有何不同?
根据Google的概述,Antigravity App是一个独立的桌面应用程序,用于启动和检查AI代理团队。 其代理返回工件,如实施计划、演练和截图,以便在关键里程碑进行评审。
AI编码助手指南解释了为什么一个增加包名称IronWebScraper的任务是如此刻意。 它还列出了IronWebScraper供代理使用的文档文件,并包含单一提示的验收测试。
您应该在计划、代码更改和演练中检查什么?
计划应将网页缓存和保存的爬取状态放在工作目录下,并应当说明停止时排队的请求会在保存的状态中列出。
在代码更改中,找寻反射。 测试运行通过反射调用非公开的IronWebScraper成员,停止了一个爬取并稍后读取了缓存页面,首次提示后使用13次BindingFlags.NonPublic,在结束时使用9次。 这些成员在API参考文档中不存在,并且名称被混淆,因此需要请求公开API并确认每个成员在API参考文档中。
演练应该包含由浏览器打开的每个工作页面的截图。 Antigravity App将每个截图作为图像工件存储,这些工件可接受评论。 在CLI中,命名文件夹的提示(如artifacts/shots),将屏幕截图保存在存储库中。
如何在浏览器中检查记录与公告板的对比?
/browser Check the crawl against the board as a reader sees it.
Delete output/, cache included, and run the crawler once with no options. Then, in the browser:
1. Open http://localhost:5481/jobs/page-1.html and go through all six listing pages.
2. Count the job cards on each page.
3. Open J-1004, J-1012, J-1025, J-1047 and J-1058, and save a screenshot of each in artifacts/shots.
4. Note every field as the page displays it.
Put two tables in the Walkthrough and in artifacts/browser-check.md:
- job cards on the board against lines in output/jobs.jsonl
- one row per field of those five jobs, with the page value, the record value and a match column
If something differs, trace it in the crawler code before editing.
Then fix it, delete output/, crawl again and add both tables a second time.
Tell me what differed and what you changed.
公告板上有六个列表页面,包含60个工作。 七个列表在发布后被编辑,每个列表在页面显示的工资前都有一个隐藏元素保存其旧工资。 J-1004、J-1012和J-1047在其中。 读取第一个dd.salary元素的爬虫记录了旧的数字,而浏览器显示了新的数字。
J-1004是/browser子代理在测试运行中捕获的。
输出
下面的文件是爬取在浏览器比较后写入的jobs.jsonl,未作更改。
来自测试板块的60条工作记录,每行一个JSON对象,并显示J-1004的工资。
网页缓存和保存状态保存了什么?
EnableWebCache在工作目录的WebCache文件夹中为每个URL写入一个.webcache文件。 对于测试板块的66页,探测器测量了66个文件和32,341字节。 重跑对板块没有请求,运行时间缩短为4.6秒而不是9.4秒。API中的CretaedFromWebCache标志在每个响应中都为true。
缓存不会检测到站点的更改。在板块添加了两个工作和改变了一份工资后,缓存的重跑没有请求并写入了旧的列表。 EnableWebCache与TimeSpan限制了这一点,在15秒过期时间内,20秒后重跑重新获取了全部66页。
Start在爬取ID下将两个文件写入SavedState文件夹,一个列出每个排队请求,另一个列出收到响应的请求。 经过6秒的爬取后,两个文件都存在,尽管XML文档描述为每五分钟保存一次。 完成的爬取ID再次启动,只进行了一次请求,并没有写入任何内容,因此重用同一ID的每日任务不会收集新工作。
ScrapeUnique从未将每个爬虫传递给文件的第一个记录写入,因此6个60份工作探测爬取中有6个写入了59行。
您如何设置Antigravity可以工作的项目?
What IronWebScraper version does HarborJobs.Crawler.csproj pin? Quote the PackageReference.
Quote the lines that apply the license key as well.
Judge everything from the code, without starting the crawler.
The key handling should meet these rules:
- The crawler's own code takes the key only from IRONWEBSCRAPER_LICENSE_KEY.
- No appsettings file or launch profile contains it.
- Without that variable, the crawler prints one line naming it and exits with code 2.
- A licensing failure is reported by exception type name, never by its message.
If the missing-variable line isn't there, add it to Program.cs and touch nothing else.
For every other rule that fails, tell me the file and line.
没有许可证密钥,IronWebScraper 2026.10.1在Start中抛出LicensingException,而在Init运行前。 异常类型不是公开的,因此catch (LicensingException)由于CS0122无法编译,处理程序必须通过名称匹配类型。
ObeyRobotsDotTxt仅从默认端口读取robots.txt,因此运行中测试代理将板块的禁用路径添加到BannedUrls中。
如何保存爬取规则用于后续任务?
当浏览器比较通过时,将会话的决定记录为规则。 根据Antigravity的规则文档,CLI从存储库的根目录和子目录中读取AGENTS.md、GEMINI.md和.agents/rules。 /learn指令将会话中的更正汇总为持久性规则。
/learn Write down what this session settled, in AGENTS.md at the repo root.
Use the heading "Crawling Harbor Jobs" and cover these points:
- the board URL, the 250 ms limit, robots.txt and the user agent
- the folders for the cache and the crawl state, and when to delete them
- which selector gives the salary the page displays
- that the key comes only from IRONWEBSCRAPER_LICENSE_KEY and is never printed or stored
- that no command lists environment variables
- that a crawl is finished once the browser comparison matches
Show me AGENTS.md when it's written.
爬取中途停止时会发生什么?
Stop在IronWebScraper 2026.10.1中未能干净地结束爬取。其XML摘要称停止是优雅的,并说爬取可以在其爬取ID下重新启动而不丢失数据。 在parse方法或其他线程调用Stop后,Start和StartAsync在3个探测中都未返回,这些探测在60到150秒后结束。
在相同的爬取ID下重新启动也未完成爬取。 在一个停止于20份工作的10个探测重启中,Start每次都正常返回,文件包含60个工作中的19到57个。 在9个检查其保存状态的重启中,1到19个排队请求仍没有响应。
/plan A crawl that stops halfway must finish on the next start, with every job exactly once.
Write the Implementation Plan first, and wait until I approve it before you edit any file.
After I approve, measure a restart before you fix anything:
1. Delete output/.
2. Run the crawler with --stop-after 20.
3. Run it again with no options.
4. Count the lines in output/jobs.jsonl, the repeated ids and the ids missing from J-1001 to J-1060.
5. Have the crawler log the pages each run took from the board and from the cache.
6. Write those counts to output/run-counts.txt.
Repeat steps 1 to 6 three times. Treat a run still going after two minutes as hung, and kill it.
Then fix every cause you found, keep the web cache, and do the three restarts again.
Add what the restarts showed to AGENTS.md.
Then tell me:
- the counts for each run, before and after the fix
- what output/SavedState contained after each stop, or that the fix no longer uses it
该示例仅使用IronWebScraper的公开API,并在IronWebScraper 2026.10.1上编译。在对测试板块的三个停止和重启周期中,最多请求停止前的22页和重启后的其余44页,其文件每次只保存60个工作。 在第三个周期后,输出文件夹包含jobs.jsonl和WebCache中的66个缓存页面,且示例未删除任何内容。
using System.Text.Json;
using IronWebScraper;
var key = Environment.GetEnvironmentVariable("IRONWEBSCRAPER_LICENSE_KEY");
if (string.IsNullOrWhiteSpace(key))
{
Console.WriteLine("Missing required environment variable: IRONWEBSCRAPER_LICENSE_KEY");
return 2;
}
License.LicenseKey = key;
int stopAfter = args.Length == 2 && args[0] == "--stop-after" ? int.Parse(args[1]) : 0;
new HarborJobsScraper(stopAfter).Start();
return 0;
public sealed class HarborJobsScraper(int stopAfter) : WebScraper
{
private const string Board = "http://localhost:5481";
private readonly object _fileLock = new();
private readonly HashSet<string> _written = new();
private int _jobsThisRun;
public override void Init()
{
WorkingDirectory = "output";
RateLimitPerHost = TimeSpan.FromMilliseconds(250);
ObeyRobotsDotTxt = true;
AllowedDomains.Add("localhost");
Identities.Add(new HttpIdentity { UserAgent = "HarborJobsCrawler/1.0" });
// Pages fetched before a stop come back from output/WebCache on the next start.
EnableWebCache(TimeSpan.FromHours(12));
string jobsFile = Path.Combine(WorkingDirectory, "jobs.jsonl");
if (File.Exists(jobsFile))
{
foreach (var line in File.ReadLines(jobsFile))
{
_written.Add(JsonDocument.Parse(line).RootElement.GetProperty("id").GetString()!);
}
}
Request(Board + "/jobs/page-1.html", Parse);
}
public override void Parse(Response response)
{
foreach (var link in response.Css("article.job-card h2 a"))
{
Request(response.ToAbsoluteUrl(link.Attributes["href"]), ParseJob);
}
foreach (var next in response.Css("a[rel=next]"))
{
Request(response.ToAbsoluteUrl(next.Attributes["href"]), Parse);
}
}
public void ParseJob(Response response)
{
// The page carries an old salary in a hidden element ahead of the one it displays.
var salary = response.Css("dd.salary").Last(n => n.GetAttribute("style") is not { } s || !s.Contains("display:none"));
var job = new
{
id = response.Css("dd.job-id")[0].TextContentClean,
title = response.Css("h1.job-title")[0].TextContentClean,
company = response.Css("dd.company")[0].TextContentClean,
location = response.Css("dd.location")[0].TextContentClean,
salary = salary.TextContentClean,
postedDate = response.Css("time")[0].GetAttribute("datetime"),
closed = response.CssExists("p.closed"),
};
lock (_fileLock)
{
// ScrapeUnique drops the first record of each process, so the crawler writes the file itself.
if (_written.Add(job.id))
{
File.AppendAllText(Path.Combine(WorkingDirectory, "jobs.jsonl"), JsonSerializer.Serialize(job) + "\n");
}
if (stopAfter > 0 && ++_jobsThisRun >= stopAfter)
{
// Start never returns after Stop() in 2026.10.1, so a test stop ends the process.
Environment.Exit(0);
}
}
}
}
何时应指派多个代理?
Harbor Jobs adds jobs every morning, and the crawler is scheduled once a day.
Divide the next part between two agents.
Agent A builds an xUnit project, HarborJobs.Crawler.Tests:
- Save the J-1004, J-1012, J-1021, J-1047 and J-1061 pages from today's board into TestData.
- Test the record each saved page produces, salary and closed flag included.
- The tests read only those files and send no request.
- Keep the test project out of the crawler's compile items.
- Run it with dotnet test HarborJobs.Crawler.Tests.
If the tests fail, stop there and tell me why.
Otherwise Agent B takes these steps:
1. Run the crawler with today's command and the existing output/ folder, as tomorrow's run would.
2. Use /browser to open page 1 of the board and each job it links to.
3. Check that every one of those jobs has a line in jobs.jsonl with the same values.
If the board and the file disagree, explain why before changing anything.
A restart on the same day still reads the cache, and the fix has to keep that.
Save screenshots in artifacts/shots-day2.
Write how many page 1 jobs matched before and after to artifacts/day2-check.md.
Tell me what each agent found.
在此提示之前,测试板块切换到第二天早晨的列表,页1的顶部有两个新工作,一个工资变更和一个关闭的工作。
在实际运行中这些提示生成了什么?
所有六个提示都在一个主要Antigravity CLI对话中无人值守地运行,跳过了权限提示。 下面的每个数据来自时间日志和对运行中的提交、请求日志和重跑的独立检查。
- 在第一个提示之前,存储库中只有打印其名称的控制台项目。
- 在计划过程中,代理通过自身的PowerShell请求提取了工作页面并找到了隐藏的工资。
- 第一次完整爬取写了60行,所有60行都匹配了看板。
- 第一个停止选项的测试从未退出,最后由操作员在23分钟后中止。
- 在第一个提示中,爬虫请求间的273个间隔中有64个低于240毫秒,最短的为8毫秒。
- 从第二个提示开始,所有爬虫间隔都未低于292毫秒。
- 浏览器比较将60个工作卡对比60行,并发现没有字段差异,因此其修复步骤无所更改。
- 浏览器子代理的Chrome请求了不允许的/apply/下的125个页面,而爬虫没有请求。
- 没有关键变量,最终爬虫打印了一行并以代码2退出。
- 用无效的密钥结束时出现未处理的LicensingException,安装提示报告了该故障并留在原处。
- 重启修复抛弃了爬取ID并保留了自己待处理页面的队列。
- 在三个独立的停止和重启周期中,最终爬虫共写入了60个工作,停止前23次请求,停止后44次请求。
- 在第二天的公告板上,第一个提示的爬虫未作请求,并漏掉了两个新工作。
- 该爬虫的文件在新的页1上匹配了10个工作中的7个。
- 在代理B的修复后,最终爬虫匹配了该公告板上的全部60个工作。
- 代理A的五次测试都在不联系公告板的情况下通过。
- 有些步骤与页面中的运行不一样。 第一个提示的57.2分钟包括等待挂起的停止测试。第二个提示起的每个提示在全部回复后以agy错误状态结束,因为函数调用错误格式。 代理A作为单独的子代理运行,主对话运行代理B的步骤与浏览器子代理。 /browser子代理使用运行启动的无头Chrome,因为CLI寻找用户配置文件中的Chrome DevTools端口文件。最终爬虫在完整爬取后清除了其缓存,因此在完成后的重跑再次请求每个页面。
ChatGPT Codex从AGENTS.md读取其固定规则,就像本指南一样,并将任务的命名部分交给探险和工人代理。 GitHub Copilot也会在不请求站点的情况下测试爬虫对保存页面。Antigravity的版本、模型、运行时和测试机器记录在共享的运行记录中。
故障排除
- 高级网页抓取功能用于网页缓存、爬取ID和限流
- 应用许可证密钥当
Start在第一个请求前抛出异常时 - IronWebScraper & JavaScript当浏览器显示一个爬虫从未接收到的值时
- 线程安全讨论IronWebScraper如何管理其爬取线程
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。