代码之家  ›  专栏  ›  技术社区  ›  xian

在C中使用网页的库#

  •  0
  • xian  · 技术社区  · 15 年前

    我正在寻找一个库,它可以让我使用C处理网页,而无需以图形方式显示任何内容。库应该处理使用javascript/ajax的网站,它应该返回正确的HTML,就好像我是从firefox/chrome中查看源代码一样。

    1 回复  |  直到 15 年前
        1
  •  1
  •   xian    15 年前

    我已经弄明白了。结果发现我根本不需要图书馆,我可以用 WebBrowser 控制。

    using System;
    using System.Windows.Forms;
    
    namespace WebBrowserDemo
    {
        class Program
        {
            public const string TestUrl = "http://www.w3schools.com/Ajax/tryit_view.asp?filename=tryajax_first";
    
            [STAThread]
            static void Main(string[] args)
            {
                WebBrowser wb = new WebBrowser();
                wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
                wb.Navigate(TestUrl);
    
                while (wb.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }
    
                Console.WriteLine("\nPress any key to continue...");
                Console.ReadKey(true);
            }
    
            static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                WebBrowser wb = (WebBrowser)sender;
    
                HtmlElement document = wb.Document.GetElementsByTagName("html")[0];
                HtmlElement button = wb.Document.GetElementsByTagName("button")[0];
    
                Console.WriteLine(document.OuterHtml + "\n");
    
                button.InvokeMember("Click");
    
                Console.WriteLine(document.OuterHtml);           
            }
        }
    }