c# - HtmlAgilityPack 获取 Title 和 meta

标签 c# html-agility-pack title

我尝试练习“HtmlAgilityPack”,但我遇到了一些问题。这是我编码的内容,但我无法正确获取网页的标题和描述...... 如果有人能告诉我我的错误:)

...
public static void Main(string[] args)
    {
        string link = null;
        string str;
        string answer;

        int curloc; // holds current location in response 
        string url = "http://stackoverflow.com/";

        try
        {

            do
            {
                HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
                HttpWReq.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                //url = null; // disallow further use of this URI 
                Stream istrm = HttpWResp.GetResponseStream();
                // Wrap the input stream in a StreamReader. 
                StreamReader rdr = new StreamReader(istrm);

                // Read in the entire page. 
                str = rdr.ReadToEnd();

                curloc = 0;
                //WebPage result;
                do
                {
                    // Find the next URI to link to. 
                    link = FindLink(str, ref curloc); //return the good link
                    Console.WriteLine("Title found: " + curloc);
                    //title = Title(str, ref curloc);

                    if (link != null)
                    {
                        Console.WriteLine("Link found: " + link);
                        using (System.Net.WebClient client = new System.Net.WebClient())
                        {
                            HtmlDocument htmlDoc = new HtmlDocument();
                            var html = client.DownloadString(url);
                            htmlDoc.LoadHtml(link); //chargement de HTMLAgilityPack
                            var htmlElement = htmlDoc.DocumentNode.Element("html");

                            HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//meta[@name='description']");
                            if (node != null)
                            {
                                string desc = node.GetAttributeValue("content", "");
                                Console.Write("DESCRIPTION: " + desc);
                            }
                            else
                            {
                                Console.WriteLine("No description");
                            }

                            var titleElement =
                                                htmlDoc.DocumentNode
                                                   .Element("html")
                                                   .Element("head")
                                                   .Element("title");
                            if (titleElement != null)
                            {
                                string title = titleElement.InnerText;
                                Console.WriteLine("Titre: {0}", title);
                            }
                            else
                            {
                                Console.WriteLine("no Title");
                            }
                            Console.Write("Done");
                        }
                        Console.Write("Link, More, Quit?");
                        answer = Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("No link found.");
                        break;
                    }
                } while (link.Length > 0);

                // Close the Response.
                HttpWResp.Close();
            } while (url != null); 
        }
catch{ ...}

提前致谢:)

最佳答案

这样去做:

HtmlNode mdnode = htmlDoc.DocumentNode.SelectSingleNode("//meta[@name='description']");

              if (mdnode != null)
              {
                 HtmlAttribute desc;

                 desc = mdnode.Attributes["content"];
                 string fulldescription = desc.Value;
                 Console.Write("DESCRIPTION: " + fulldescription);
              }

关于c# - HtmlAgilityPack 获取 Title 和 meta,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606247/

相关文章:

c# - 使用 HtmlAgilityPack 获取 href innertext

WPF 将 TextBlock 绑定(bind)到窗口的标题

android - 如何为自定义微调器设置默认标题?

c# - 向窗口句柄发送消息

c# - Visual Studio 智能感知搞砸了

c# - 使用原始参数构造函数注册类型?

c# - MVVM View 模型引用 View

c# - 使用 HtmlAgilityPack 设置文本区域值

.net - htmlagilitypack 选择带有指定内部文本的 <a> 标签

css - CSS 中的不规则形状