c# - 传递 HTML 页面字符串并使用 HtmlAgilityPack 进行抓取

标签 c# vb.net html-agility-pack

为什么会出现此错误?

"Illegal characters in path" at htmlDoc.Load(pageSource)

pageSource 是 HTML 页面的字符串变量。我需要将页面源作为字符串传递,而不是作为文件而不是 URL。我该怎么做?

Dim ids As New List(Of String)()
Dim pageSource = getHtml(url)

Dim htmlDoc As HtmlDocument = New HtmlDocument()

htmlDoc.OptionFixNestedTags = True


htmlDoc.Load(pageSource)


Dim s As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes("//div/@id")

For Each div As HtmlNode In s
    ids.Add(div.Id)
Next

最佳答案

使用LoadHtml代替Load:

htmlDoc.LoadHtml(pageSource)

See also the source.

关于c# - 传递 HTML 页面字符串并使用 HtmlAgilityPack 进行抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589469/

相关文章:

c# - 我可以在 Mysql 的一个连接字符串下创建多个不同的插入查询吗?

.net - IO.FileNotFoundException 但文件应该存在

c# - 无法使用 HtmlAgilityPack 和 XPath 提取 <link> 元素

C# 从 wiki 页面抓取数据(屏幕抓取)

c# - MVVMLight 和平台特定的服务实现

c# - 将 BOT 与 ASP.Net 应用程序集成

c# - 单元测试表以确定有多个记录

c# - 什么在 Visual Basic 中允许而在 C# 中被禁止(反之亦然)?

.net - Entity Framework ExecuteSqlCommand Null 参数给出错误

javascript - Html 节点内的信息不可见