如何使用IronWebScraper搭配Google Antigravity
招聘分析團隊每天早上從區域招聘網站收集所有清單。 當伺服器重啟、筆記本電腦休眠或有人按下Ctrl+C時,一次爬取數百頁的過程可能會在中途停止。 當爬蟲重新啟動時,它必須在不重複下載已完成頁面的情況下完成剩下的頁面,也不會重複寫入工作。爬取也可能在沒有錯誤的情況下結束,仍然記錄頁面不再顯示的薪水。 這些錯誤都不會在控制台日誌中出現。
Google Antigravity可以按照求職者看到的方式檢查爬取的頁面。 其/browser命令會啟動瀏覽器子代理,打開每個頁面並截取螢幕截圖。 IronWebScraper保留網路緩存,並且可以在爬取ID下重新啟動爬取,這些功能在其高級Web爬取功能教程中介紹。 本指南構建了一個招聘網站爬蟲,並用已知的60個清單的當地副本測試它。
本指南適用於使用AI代理寫.NET爬蟲的開發者。 本指南中的每次爬取針對的是本地主機提供的測試板,並不涉及您操作以外的網站爬取。
快速開始在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# 網頁擷取程式庫
- 在一個構建的控制台專案上啟動代理,其中包含本地主機上的測試網站
- 給爬取分配工作目錄、網路緩存以及在N份工作後停止的方法
- 讓/browser比較工作的頁面與輸出文件
- 停止並重新啟動爬取,然後在API參考中確認每個成員
Google Antigravity是什麼,它與聊天助手有何不同?
根據Google的概述,Antigravity App是一個獨立的桌面應用程式,用於啟動和檢查AI代理團隊。 其代理會返回工件,例如實施計劃、演示和截圖,以便在關鍵里程碑處進行審查。
AI編碼助手指南解釋了會準確添加封裝名稱IronWebScraper的任務的原因。 它還列出了IronWebScraper的代理文檔文件,並包含了一次性提示驗收測試。
在計劃、代碼變更和流程演示中,您應該檢查些什麼?
計劃應將網路緩存和保存的爬取狀態放在工作目錄下,並指出停止會將排隊請求列在保存狀態中。
在代碼變更中查找反射。 測試運行通過反射停止了一次爬取,然後調用非公共IronWebScraper成員來閱讀緩存頁面,第一次提示後使用BindingFlags.NonPublic13次,結尾使用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元素的爬蟲會記錄舊數字,而瀏覽器顯示新數字。
/browser子代理在測試運行中截取了J-1004。
輸出
下面的文件是爬取在瀏覽器比較後寫的jobs.jsonl,未更改。
從測試看板中提取的60條工作記錄,每行一個JSON物件,顯示的J-1004薪水。
網路緩存和保存的狀態保存了什麼?
EnableWebCache在工作目錄的WebCache資料夾中每個URL寫一個.webcache文件。 對測試看板的66個頁面,探針測量了66個文件和32,341字節。 重新運行未對看板發出請求且完成的時間為4.6秒而不是9.4秒。CretaedFromWebCache標誌在API中如此拼寫,對每個回應都為真。
該緩存不會檢測到網站上的更改。當看板添加兩個工作並更改一個薪水後,緩存的重新運行未發出請求並寫入了舊的清單。 EnableWebCache與TimeSpan限制了這一點,並且在15秒過期後的20秒掃描中重新抓取了所有66個頁面。
Start使用爬取ID將兩個文件寫入SavedState資料夾,其中之一列出了排隊的每個請求,另一個列出了已回應的請求。 一個運行六秒的爬取後兩個文件都存在,儘管XML文檔描述為每五分鐘保存一次。 一個完成後開始的新爬取ID只發出了一次請求且未寫入任何內容,因此使用一個ID的日常工作收集不到新的職位。
每個爬蟲傳給它的第一個記錄ScrapeUnique從未寫入文件,因此在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會在Init運行之前從Start拋出LicensingException。 異常類型為非公開,因此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下重新啟動而不丟失數據。 在Stop之後,從解析方法或其他線程調用,Start和StartAsync在3次探針中均未返回,結束時間為60到150秒不等。
相同爬取ID的重新啟動也未完成爬取。 在一個中途20個職位停止後的10次探針重新啟動中,Start每次正常返回,文件中包含19到57個職位中的60個。 在隨後檢查保存狀態的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請求了robots.txt不允許的/apply/下的125個頁面,而爬蟲未請求任何。
- 缺少密鑰變數,最終爬蟲打印了一行命名並以代碼2退出。
- 使用無效的密鑰,它以未處理的LicensingException結束,設置提示報告了故障並將其置於原位。
- 重啟修復放棄了爬取ID並保持自己的待處理頁面隊列。
- 在三個獨立的停止和重啟周期中,最終爬蟲一次寫入了所有60個職位,每次停止前有23個請求,停止後有44個。
- 在次日的看板上,第一次提示中的爬蟲未發出請求,錯過了兩個新職位。
- 該爬蟲的文件與新頁面1上的10個職位中的7個匹配。
- 經過Agent B的修復後,最終爬蟲符合該看板上的所有60個職位。
- Agent A的五次測試均在未聯系看板的情況下通過。
- 一些步驟與頁面上運行的不同。 第一次提示的57.2分鐘包括停滯的停止測試等待時間。從第二次開始的每個提示在其完整回復後以一個ag狀態為ERROR收尾,具體原因是函數調用格式錯誤。 代理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。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。