【问题标题】:WebClient Wikipedia illegal path characters even after url encodeWebClient Wikipedia 非法路径字符即使在 url 编码之后
【发布时间】: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


【解决方案1】:

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

html.LoadHtml(content);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多