2017-05-18 24 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);