为什么HTML敏捷性包HtmlDocument.DocumentNode为空? [英] why HTML Agility Pack HtmlDocument.DocumentNode is null?

查看:136
本文介绍了为什么HTML敏捷性包HtmlDocument.DocumentNode为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code更改HTML流的href属性。

I'm using this code to change the href attribute of a HTML stream.

首先,我下载使用这个code完整的HTML网页(网址为网页地址)

first I download a full html page using this code:(URL is webpage address)

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse myHttpWebResponse = 
                         (HttpWebResponse)myHttpWebRequest.GetResponse();

Stream s = myHttpWebResponse.GetResponseStream();

然后我处理这个:

then I process this:

HtmlDocument doc = new HtmlDocument();

doc.Load(s);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("/a"))
{
    string att = link.Attributes["href"].Value;
    link.Attributes["href"].Value = "http://ahmadalli.somee.com/default.aspx?url=" + att;
}
doc.Save(s);

取值是HTML流。

但我有一个例外,说 doc.DocumentNode 为空!

but I've got an exception that says doc.DocumentNode is null!

我试过很多网站,但 doc.DocumentNode 为NULL

i tried many sites but doc.DocumentNode is null to

推荐答案

这为我工作。

using(WebClient client = new WebClient())
{
    client.Encoding = System.Text.Encoding.UTF8;
    var doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(client.DownloadString("http://www.google.com?q=stackoverflow"));
    foreach (var href in doc.DocumentNode.Descendants("a").Select(x => x.Attributes["href"]))
    {
        if (href == null) continue;
        href.Value = "http://ahmadalli.somee.com/default.aspx?url=" + HttpUtility.UrlEncode(href.Value);
    }
    StringWriter writer = new StringWriter();
    doc.Save(writer);
    var finalHtml = writer.ToString();
}

另请参阅 HttpUtility.UrlEn code 才能够回到正确获得url。否则,原始地址某些参数可能会引起问题。

Also see the HttpUtility.UrlEncode to be able to get the url back correctly. Otherwise, some parameters in original url may cause problem.

使用 HttpUtility.UrlDe code 脱code吧。

这篇关于为什么HTML敏捷性包HtmlDocument.DocumentNode为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆