c# - 使用 C#,如何检测断开的链接或标记?

标签 c# html error-handling html-agility-pack

我有一个语法不正确的 HTML 文件,我正在用 HTML Agility Pack 解析它.

但是如果我有一个像

这样的链接

<a href="http://google.com/!/!!!">Google</a>

这是一个问题,有没有一种可能的方法来检测损坏的链接,以便在发现错误时(该链接上没有可用页面)应用程序将该链接存储在列表中并返回它?

同样的标签问题,例子:

<img hhh="jjj"/>

这里图片标签全错了,这个也应该在'errors for repair'列表中。

提前致谢。

最佳答案

你需要遍历Document.DocumentNode.Descendants("a")并检查是否href标签有问题。

同样,你可以遍历Document.DocumentNode.Descendants("img")并检查 src属性。

编辑:

要检查不良属性,您可以维护一个 Dictionary<string, IEnumerable<string>>将标记名称映射到有效属性,然后使用 LINQ 查找缺失的属性,如下所示:

from tag in Document.DocumentNode.Descendants()
let legalAttributes = allAttributes[tag.TagName]
from attribute in tag.Attributes
where !legalAttributes.Contains(attribute.Name, StringComparer.OrdinalIgnoreCase)
select new { Tag = tag.OuterHtml, Attribute = attribute.Name }

关于c# - 使用 C#,如何检测断开的链接或标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863483/

相关文章:

javascript - 当目光移开时刷新 iframe(从 iframe 本身内部)

javascript - 删除特定 div 内的 <br> 标签

html 和 css : aligning a select to the right edge of a table

php - 如何将 PHP 回溯保存到错误日志中?

c# - 如何从自定义授权策略向客户端发送有意义的错误

c# - 从日期选择器的下拉列表中禁用日期

c# - WPF c# webbrowser 在顶部菜单上滚动

Python 为 os.listdir 返回的文件名引发 FileNotFoundError

c# - 所需的防伪 cookie 不存在错误 : When registering first user in new asp. 仅 Chrome 中的 net 5 项目

c# - TemporaryKey.pfx 的问题我没有密码