如何使用IronWebScraper與GitHub Copilot

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

電影放映時間的聚合器將幾個場地的放映集合到一個文件中,那個列表應用程式每晚讀取。 每個場地都在自己的頁面上發布它的節目,同一部電影可以同時在三個場地放映。 被爬蟲錯過的放映根本不會出現在應用程式中。

GitHub Copilot 可以通過在 Visual Studio 和 VS Code 中的聊天和代理模式,並在終端中通過 Copilot CLI 與存儲庫協作。 GitHub 的雲代理將指派的問題轉化為拉取請求。IronWebScraper 通過 .NET 的一個子類從覆蓋 WebScraper、Init 和 Parse 爬取網站。 提交到存儲庫的指令文件將爬蟲的規則帶到這些位置的每個地方。

本指南介紹如何引導 Copilot 構建並維護爬蟲。 電影網站指南描述了爬蟲遵循的列表頁面、詳細頁面和MetaData模式。 此處描述的每次爬蟲都針對在本地機器上提供的測試網站,從不針對電影的實時網站。

快速開始
NuGet使用NuGet安裝

PM > Install-Package IronWebScraper

在 NuGet 包管理器控制台中運行上述命令安裝 IronWebScraper,或在 NuGet 包管理器中搜索該包。

在儲存庫根目錄打開終端,啟動Copilot CLI並提交提示。 @ 提到將命名文件附加到 Copilot CLI,Visual Studio 中的 Copilot Chat 則使用 # 引用。

Build the showtimes crawler in @src/Showtimes.Scraper/ShowtimeScraper.cs.
It collects every screening from the cinema test site at http://localhost:5447/.
Directory.Packages.props already pins IronWebScraper 2026.10.1.

The crawler should do this:
- Start at the site's start page and follow each venue's pages, page 2 included.
- Write one line per screening to out/showtimes.jsonl, using the Screening class.
- Fill the venue-page fields as @docs/output-format.md describes.
- Leave the film-page fields empty for now.

Then do these steps in order:
1. Add a crawl command to @src/Showtimes.Cli/Program.cs that takes --site <url> and --out <path>.
2. Run dotnet build.
3. In the repo root, run crawl --site http://localhost:5447/ --out out/showtimes.jsonl.

Tell me how many screenings each venue has.
Text

提示不命名選擇器,因為 docs/output-format.md 列出了每個字段的來源元素。

在 Copilot 運行的環境中設置 IRONWEBSCRAPER_LICENSE_KEY,並讓程式將其拷貝到 License.LicenseKey。 主控指南 選擇了 IronWebScraper 自行讀取的變量,而 授權金鑰指南 列出了其他支援的位置。



如何為 Copilot 設置 IronWebScraper 規範?

將項目範圍內的規則放在.github/copilot-instructions.md中。 GitHub的支援參考列出了該檔給Visual Studio、VS Code和JetBrains IDEs中的Copilot Chat,以及雲代理和Copilot代碼審核。 Copilot CLI從相同位置讀取。

This repo holds the showtimes crawler, which gathers cinema screenings into one file.
Follow these rules in every change:
- Use IronWebScraper for all crawling and HTML parsing, never HttpClient.
- Don't add AngleSharp, HtmlAgilityPack or another scraping package.
- Change the IronWebScraper version in Directory.Packages.props only in an upgrade task.
- The showtimes crawler and its tests read IRONWEBSCRAPER_LICENSE_KEY into License.LicenseKey.
- IronWebScraper won't read that variable name on its own, so set the key before Start.
- Keep the key out of all output, logs included.
- Don't list environment variables.
- Crawl only the site a task names, which is http://localhost:5447/ in development.
- Tests crawl TestData/site through a local HTTP listener and never touch that address.
- Keep RateLimitPerHost at one second.
- Write records with WebScraper.Scrape, not with a JSON writer of your own.
- Treat docs/output-format.md as the contract for out/showtimes.jsonl.
- If the contract looks wrong, tell me and leave the file alone.
- Read the IronWebScraper docs for agents at https://ironsoftware.com/webscraper/csharp/llms.txt
Markdown

速率限制規則設置了每次爬蟲請求之間的間隔。 在預設 RateLimitPerHost 為50毫秒時,一次探測爬蟲在0.048到0.064秒之間到達測試網站。 在一秒鐘時,間隔測量為1.02到1.10秒。

如何為爬蟲提供自己的規則?

諸如 .github/instructions/scraper.instructions.md 之類的檔案中包含樹形結構中applyTo模式命名部分的規則,這裡是爬蟲及其測試。 根據GitHub的文檔,Copilot CLI僅在它處理匹配這些模式的檔案時才包括該檔。

---
applyTo: "src/**/*,tests/**/*"
---

The crawler and its tests rely on IronWebScraper 2026.10.1, which behaves like this:
- A crawl fetches each URL once per parse method.
- A second Request for that URL with the same parse method is dropped, even with new MetaData.
- MetaData passes the parse method the same object instance, not a copy.
- When a parse method throws, the page is fetched and parsed again, up to 8 more times.
- Each retry repeats every Scrape call made before the throw.
- A URL that answers 500 is requested 9 times, and its parse method is never called.
- Css throws an XPathException on a selector with two > combinators, such as div > p > a.
- TextContentClean returns entities such as &eacute; unchanged.

Follow these rules in this code:
- Request each film page once and copy its details onto every screening of that film.
- Decode text from TextContentClean with WebUtility.HtmlDecode.
Markdown

該檔案中列出的每個行為都來自對測試網站的探測。

IronWebScraper 如何處理由三個場地鏈接的電影頁面?

場地頁將每部電影鏈接到一個電影頁,因此三個展示 Harbour Lights 的場地鏈接到相同的 URL。

Fill in the film-page fields on every line the crawler writes.
They are RuntimeMinutes, Genres and Rating, and @docs/output-format.md gives the source of each.
Follow this guide for the detail requests:
https://ironsoftware.com/webscraper/csharp/how-to/scraping-from-an-online-movie-website/

Two things about the test site matter here:
- Harbour Lights plays at all three venues, and all 6 of its screenings need the runtime.
- The Night Train to Arles page answers 500 on purpose.

Keep the Night Train to Arles screening, with its film fields empty.
Then run the crawl again with --out out/details/showtimes.jsonl.
Tell me how many lines got a runtime, and which film's lines didn't.
Text

IronWebScraper 對整個爬蟲中的每個解析方法請取一次 URL。 在探測中,三個場地用相同的解析方法和不同的 MetaData 請求了 Harbour Lights 頁面,網站只收到一個請求。解析方法只查看了第一個場地的 MetaData,因此其他兩個場地的放映從未收到詳細信息。 用不同解析方法請求相同的 URL 時再次獲取了,並且具有不同查詢字串的頁面也是如此。

電影指南的詳細解析器在指南自己的標記上失敗。它的類型選擇器 div > p > a 和它的演員選擇器 div > p:nth-child(2) > a 都引發了 XPathException,因為 IronWebScraper 拒絕了擁有兩個子結合器的選擇器。 擁有一個結合器的選擇器(如 div.mvici-left > p)工作正常。

IronWebScraper 重試解析方法拋出異常的頁面,正如它重試加載失敗的頁面一樣。 在探測中,解析方法對一個頁面被調用了9次,並且在拋出前進行的 Scrape 呼叫寫了9行。

輸出

在瀏覽器中,Riverside 場地頁面顯示其名稱帶有重音字母。 頁面來源將該字母作為命名的 HTML 實體寫入,而 TextContentClean 返回的實體保持不變。

本地測試網站的 Riverside 場地頁面,包含三部電影及其放映時間

截取於 Microsoft Edge 的本地測試網站上 Riverside 場地的第1頁。網站上的所有電影和場地都是虛構的。

顯示 Harbour Lights 在三個場地上映的七行 showtimes JSONL 文件和空電影字段的 Night Train to Arles

第二個提示寫入的 out/details/showtimes.jsonl 的 Harbour Lights 及 Night Train to Arles 行,其行號在文件中。

如何在不使用實時網站的情況下測試爬蟲?

使用Shift+Tab將Copilot CLI切換到計劃模式,然後提交此提示。

Plan a new xUnit project, tests/Showtimes.Tests, listed in Showtimes.slnx.
Show me the plan, and don't write code until I say go.

The tests' local listener must answer 500 for /film/night-train-to-arles-20790/, like the test site.

Each test runs ShowtimeScraper and checks one of these:
- Northgate Picturehouse has 10 screenings, and the other two venues have 7 each.
- The Riverside venue name has no HTML entity left in it.
- All 6 Harbour Lights screenings have a RuntimeMinutes of 112.
- The Night Train to Arles screening is still there, with a null RuntimeMinutes.

After my go, write the tests and rerun dotnet test until every one passes.
Then give me the number that pass.
Text

TestData/site 中的 robots.txt 對測試爬蟲沒有影響。 IronWebScraper 請求 robots.txt 時不帶起始 URL 的端口,所以隨機端口上的監聽器從不提供這些規則。 在端口80上的一個監聽器,探測爬蟲讀取了它們並跳過了不允許的 /booking/頁面。 Claude Code 指南 完整涵蓋了此行為。

為什麼某些運行中場地會丟失?

在這種情況下,隊友的tools/VenueCheck在每晚爬蟲之前請求三個場地頁面並報告任何不回應的場地。

tools/VenueCheck reports a venue as DOWN on some runs, though the site is up.
It checks the three venue pages listed in tools/VenueCheck/venues.json.
Find out why and fix it.

Before you fix anything, do these steps in order:
1. Run it five times against http://localhost:5447/ and note which venues came back DOWN.
2. Turn on LogLevel.All and find out what IronWebScraper does with the Request calls in Init.
3. Tell me the cause.

Keep the fix within these limits:
- Change files in tools/VenueCheck only.
- Add no sleep, and keep a single call to Start.

Then run it five more times and show me the results.
Text

IronWebScraper 不會保留 Init 中的每個請求。 從 Init 請求的三個場地 URL 中,26次探測運行中1次爬取了全部三個,其他25次爬取了一個或兩個。這些26次運行由18次探測爬蟲運行和修補前commit的8次工具/VenueCheck運行組合而成。 日誌列出了三個請求並在兩個請求後報告爬蟲完成,跳過的 URL 從未到達服務器。 從解析方法進行的請求從未丟失。從 Init 請求第一個場地,從它的解析方法請求另兩個返回了全部三個場地的8次運行中有8次。

使雲代理可以操作的問題具備什麼條件?

根據 GitHub,指派給 Copilot 的問題可以將其標題、描述、指派時存在的評論和當時給出的任何指令傳給代理。 事後添加的評論不會到達代理,因此問題主體必須包含整個請求。

Title: A second crawl must not double out/showtimes.jsonl

Files: src/Showtimes.Scraper and tests/Showtimes.Tests
Versions: net9.0 with IronWebScraper 2026.10.1

Background: IronWebScraper's Scrape appends to the file it writes and never empties it.
Without a cleanup step, a second crawl to the same --out path leaves 48 lines instead of 24.

Expected: after any crawl, the output file holds exactly one line per screening.

Constraint: the crawl command keeps its options.

Acceptance: close the issue once these are true:
- Two crawls in a row leave the same 24 lines, in any order.
- A new test crawls twice into one temp folder and counts the lines.
- dotnet test passes.

Summary: say where the old output gets cleared, and why there.
Markdown

為什麼雲代理在 IronWebScraper 項目上的測試失敗?

需要在雲代理的環境中設置用於爬蟲的授權金鑰。 GitHub 的文檔默認將該環境描述為 Ubuntu Linux,.github/workflows/copilot-setup-steps.yml 中的copilot-setup-steps任務可以準備它。 該任務在 Copilot 開始工作之前在 GitHub Actions 中運行,並且除了默認分支上的檔案外,工作流不會觸發。 當設置步驟失敗時,Copilot 跳過剩餘的設置步驟並繼續在其現狀環境中工作。

Create the cloud agent's setup workflow in .github/workflows/copilot-setup-steps.yml.
Call its job copilot-setup-steps, because GitHub skips a job with any other name.

The job needs these settings:
- The runner is ubuntu-latest.
- The workflow token has contents: read and nothing else.
- workflow_dispatch triggers it, and so does a pull request that edits the file.

Its steps go in this order:
1. Check out the code.
2. Install .NET 9.
3. Run dotnet restore, then dotnet build -c Release.
4. Look for the license key.

The key is IRONWEBSCRAPER_LICENSE_KEY, saved as an Agents secret.
The job gets it as an environment variable already, so skip the secrets context.

When the key is missing, the last step behaves like this:
- On pull_request or workflow_dispatch, it logs a warning and passes.
- On any other event, it fails with an error naming the secret.

Show me the finished YAML, and don't try to run the workflow.
Text

將密鑰作為代理秘密存儲:根據 GitHub,雲代理無權訪問 Actions、Codespaces 或 Dependabot 的秘密。 在存儲庫的設置中,打開安全性部分的秘密和變量,選擇代理,然後在秘密標籤上添加IRONWEBSCRAPER_LICENSE_KEY。 GitHub 表示這些秘密「作為環境變量暴露給 Copilot」,並可以由 Copilot 運行的腳本和工具使用,「包括你的copilot-setup-steps.yml工作流。」但沒有說明工作流步驟在沒有env映射的情況下讀取一個,這是上面提示假設的。

合併之前應檢查什麼?

運行審查作為單獨的copilot -p命令,在編寫代碼的會話之外。

/review the changes on this branch compared with main.

Check the code against these three files:
- .github/copilot-instructions.md
- .github/instructions/scraper.instructions.md
- IronWebScraper.xml in the IronWebScraper package of the NuGet cache

Also look for two patterns that neither instructions file covers:
- an Init method that calls Request more than once
- a crawl that starts with an old output file still in place

List every mismatch you find, or say that nothing came up.
Don't change any file.
Text

在實際運行中,這些提示產生了什麼結果?

七個提示無人看管地在 Copilot CLI 中運行,針對一個放映時間存儲庫和本指南中建置的本地測試網站。下面的每個數據來自輸出文件、站點的請求日誌和存儲庫,而不是 Copilot 的回復。

  • 運行之前,CLI 印出使用行並以代碼1退出。
  • 在第一個提示之後,out/showtimes.jsonl 包含了24個放映,10在Northgate Picturehouse,其他兩個場地各7場。
  • 在第二個提示的爬蟲中,除了Arles夜車收到了9個請求並回應了每個500,其他每個電影頁只收到一個請求。
  • 第二個提示中的文件包含24行,其中23行有運行時間。
  • Arles夜車的放映仍然存在,運行時間、類型列表和評級為空。
  • 所有6次 Harbour Lights 的放映在三個場地上均包含112分鐘運行時間。
  • 在計劃模式中,Copilot寫了一個計劃,未更改任何檔,等待批准訊息。
  • 第三個提示的4個測試在沒有對測試網站進行單一請求的情況下通過。
  • 修復之前,VenueCheck 在5個檢查運行中有4個報告一個或兩個場地宕機。
  • 修復後在Init留下了一個Request調用,並從解析方法請求每個下一個場地。
  • ——三個場館在10次驗證中均答對10次。
  • 第一道場回答500時,修復的 VenueCheck 在3次運行中報告所有三個場地宕機。
  • 另外兩個場地頁面在那些運行中沒有收到請求。
  • 問題會話添加了新測試,保持爬蟲不變,因為自第一個提示起,它的Init已刪除舊的輸出。
  • 最終存儲庫中的所有5個測試在設置密鑰時通過,在不設置密鑰時失敗。
  • 審查會話報告沒有不匹配並且沒有更改任何文件。在先前的提示之後它搜索的任何模式均未保留,因此這次運行未測試審查是否找到它們。
  • 一些步驟與頁面上運行的不同。 問題本體去了新的Copilot CLI會話,因為測試儲存庫沒有GitHub遠端。 設置工作流程已編寫但未運行,出於同樣的原因。

Cursor也將提交的規則限定到它們命名的路徑,而Claude Code在編輯項目之前也保留了一個批准計劃。 共享運行記錄列出了Copilot CLI釋放,模型Auto選擇,持續時間和測試機器。


疑難排解


問題?

如果您有任何疑問,請聯繫support@ironsoftware.com

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 150,061版本:2026.11剛剛發布