【发布时间】:2017-05-18 16:34:16
【问题描述】:
我正在尝试使用 HTML Agility 包从 Wikipedia 进行简单的屏幕抓取。我复制了在我的浏览器中有效的正确链接的格式,https://en.wikipedia.org/wiki/Nanticoke%2c_Pennsylvania
原地址:https://en.wikipedia.org/wiki/Nanticoke,_Pennsylvania
更新 - 在调用 html.Load() 之前将网站下载到字符串变量不会引发异常。我在 html.Load() 上得到非法路径字符
var client = new WebClient();
var address = $"https://en.wikipedia.org/wiki/{HttpUtility.UrlEncode($"{name},_{state}")}";
HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.Load(client.DownloadString(address)); //Exception thrown here
【问题讨论】:
-
您能否编辑您的问题以显示 a) 生成的实际 URL 和 b) 您得到的实际异常?
-
您没有在编码后发布实际的 URL。此外,将您的
client.DownloadString移出html.Load并将其分配给一个字符串变量,这样我们就可以实际确定这是URL 的问题,还是来自维基百科的响应从而在HAP 中引发异常 -
加载需要一个 url 而不是原始 html。使用 LoadHtml 从字符串加载 html。
标签: c# webclient screen-scraping wikipedia