2017-05-18 23 views
0

我正在尝试使用HTML Agility pack进行维基百科的简单屏幕刮擦。我复制了适用于我的浏览器的链接的格式,https://en.wikipedia.org/wiki/Nanticoke%2c_Pennsylvania < - 在浏览器中工作 但是,当使用此代码时,我从WebClient类中收到非法路径字符异常。WebClient维基百科非法路径字符,即使在URL编码后

原文地址:https://en.wikipedia.org/wiki/Nanticoke,_Pennsylvania

更新 - 下载的网站给一个字符串变量调用html.Load()不会抛出异常之前。我得到html.Load()

异常数据的非法路径字符:enter image description here

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 
+0

你能编辑你的问题来显示a)生成的实际URL和b)你得到的实际异常吗? – stuartd

+0

您尚未发布编码后的实际URL。此外,将'client.DownloadString'移出'html.Load'并将其分配给一个字符串变量,以便我们确实可以确定它是否是URL的问题,或者维基百科的响应会在HAP中引发异常 – ColinM

+0

负载需求一个URL不是原始的HTML。使用LoadHtml从字符串加载html。 –

回答

0

我用错了方法。 Load会尝试将网站拉下来,LoadHtml除了已经加载的内容。

html.LoadHtml(content);