如何使用IronWebScraper结合GitHub Copilot
一个影院放映时间聚合器将多个场馆的放映信息收集到一个文件中,每晚应用程序读取此文件。 每个场馆在自己的网站上发布其节目表,而同一部电影可能同时在三个场馆上映。 爬虫遗漏的放映不会出现在应用程序中。
GitHub Copilot通过聊天和代理模式在Visual Studio和VS Code中与存储库协作,并通过Copilot CLI在终端中协作。 其在GitHub上的云代理会将分配的问题变为拉取请求。IronWebScraper通过覆盖Init和Parse来从.NET中的一个WebScraper子类爬取网站。 提交到存储库的指令文件将爬虫的规则带到每一个界面。
本指南涵盖了如何指导Copilot构建和维护爬虫。 电影网站指南介绍了爬虫遵循的列表页面、详细信息页面和MetaData模式。 此处描述的每次爬取都针对本地机器上的测试站点,而不是影院的实时网站。
在存储库根目录打开终端,启动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.
提示没有命名选择器,因为docs/output-format.md列出了每个字段的源元素。
在Copilot运行的环境中设置IRONWEBSCRAPER_LICENSE_KEY,并将其复制到License.LicenseKey。 中心指南中命名了IronWebScraper自动读取的变量,许可证密钥指南列出其他受支持的位置。
最小工作流程(5 个步骤)
- 从NuGet下载C#网页抓取库
- 将爬虫规则写入.github/copilot-instructions.md,以及一个特定路径的指令文件。
- 每次仅请求一次电影页面,并将其详细信息复制到该电影的每个放映中。
- 从Init请求一个起始页面,并从解析方法中排队其他所有页面。
- 将下一个更改分配给云代理作为一个问题,并根据IronWebScraper API参考验证拉取请求。
如何为Copilot设置IronWebScraper惯例?
将项目范围的规则放置在.github/copilot-instructions.md中。 GitHub的支持参考将该文件列为Visual Studio、VS Code和JetBrains IDE中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
速率限制规则为每次爬取中的请求间隔设置间距。 在50毫秒的默认RateLimitPerHost下,一个探测爬虫在0.048到0.064秒间隔内访问了测试站点。 在1秒时,间隔测得为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 é 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.
该文件中列出的每个行为都来自测试站点的探测。
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.
IronWebScraper对于整个爬虫,每个解析方法只获取一次URL。 在探测中,三家场馆使用相同的解析方法请求《Harbour Lights》页面,但使用不同的MetaData,最终站点只接收到一次请求。解析方法仅看到了第一个场馆的MetaData,所以其他两个场馆的放映未接收到详细信息。 相同的URL用不同的解析方法请求后再次被获取,而使用不同查询字符串的页面也是。
电影指南的详细解析器在指南自己的标记上失败。其类型选择器div > p > a和演员选择器div > p:nth-child(2) > a都抛出了XPathException,因为IronWebScraper拒绝一个具有两个子组合符的选择器。 一个组合符的选择器,例如div.mvici-left > p,是有效的。
IronWebScraper会重试解析方法抛出异常的页面,就像重试加载失败的页面一样。 在探测中,对于一个页面,解析方法被调用了9次,抛出前的Scrape调用写了9行。
输出
在浏览器中,Riverside场馆页面显示其名称带有重音符。 页面源代码将该字母写成命名的HTML实体,TextContentClean保持实体不变。

用Microsoft Edge在本地测试网站上捕获的Riverside场馆第1页。网站上的所有电影和场馆都是虚构的。

out/details/showtimes.jsonl的Harbour Lights和Night Train to Arles行,如第二个提示编写的那样,附带文件中的行号。
如何在没有实时网站的情况下测试爬虫?
使用Shift+Tab循环到计划模式,然后提交此提示。
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.
在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.
IronWebScraper不会在Init中保留每一个请求。 从Init请求的三个场馆URL中,爬虫在26次探测运行中的1次提取了全部三个,其余25次中提取了一个或两个。这26次运行结合了探测爬虫的18次运行和修复前提交的tools/VenueCheck的8次运行。 日志列出了三个请求,并在两个请求后报告爬取完成,被跳过的URL从未到达服务器。 从解析方法发出的请求从未丢失。从Init请求第一个场馆,并从其解析方法中请求另外两个场馆,在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.
为什么云代理的测试在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.
将密钥存储为代理的秘密:根据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.
在实际运行中这些提示生成了什么?
七个提示在Copilot CLI中对着一个放映时间存储库和一个为本指南构建的本地测试站点无监督地运行。以下所有数据均来自输出文件、网站的请求日志和存储库,而非Copilot的回复。
- 运行之前,CLI打印了一行用法并以代码1退出。
- 在第一个提示之后,out/showtimes.jsonl包含24场放映,10场在Northgate Picturehouse和其余两个场馆各7场。
- 在第二个提示的爬取中,每个电影页面接收到一个请求,除了到阿尔勒的夜列车,接收了9次并且每次都回答500。
- 第二个提示的文件中有24行,其中23行有播放时长。
- 夜行列车到阿尔勒的放映保持不变,没有播放时长、类型列表和评级。
- 随着6个Harbour Lights放映分布在三个场馆,均为112分钟时长。
- 在计划模式下,Copilot写下一个计划,未更改任何文件,并等待批准信息。
- 第三个提示的4个测试在未向测试站点发出一个请求的情况下通过。
- 在修复前,VenueCheck报告4次检查运行中的1至2家场馆宕机。
- 修复后,Init中留下一个请求调用,并从解析方法请求每个接下来的场馆。
- 在固定检查的 10 次运行中,三个环境均 10 次全部响应。
- 假设第一个场馆回答500,修复后的VenueCheck在3次运行中报告所有三个场馆宕机。
- 在这些运行中,其他两个场馆页面没有接收到请求。
- 问题会话中添加了新的测试,并保持爬虫不变,因为其Init自第一个提示起已删除旧输出。
- 在最终存储库中的所有5个测试在设置密钥时通过,未设置时失败。
- 审核会话未报告不匹配且未更改任何文件。其搜索的模式在早期提示后已不复存在,因此此次运行未测试审核能否找到它们。
- 有些步骤与页面中的运行不一样。 问题内容进入了一个新的Copilot CLI会话,因为测试存储库没有GitHub远程。 设置工作流被写入但未运行,出于相同原因。
Cursor也将提交的规则限定到它们命名的路径,Claude Code在编辑项目前也拥有一个审批计划。 共享运行记录列出了Copilot CLI的版本、自动选择的模型、持续时间和测试机器。
故障排除
- 如何抓取在线电影网站以获取列表和详细页面模式
MetaData。 - 应用许可证密钥以处理在
Start处抛出许可证异常的抓取。 - 线程安全为爬虫的连接和速率限制提供支持。
有问题吗?
如果您有任何问题,请联系support@ironsoftware.com
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。