使用 C# 在控制台应用程序中加载网页内容

Load web page contents in console application using c#

我想使用 C# 在控制台应用程序中加载以下网页的内容。 http://justicecourts.maricopa.gov/findacase/casehistory.aspx

使用下面的代码,我在屏幕上变得空空如也,但如果我加载 google.com 网页,它会完美运行。

通过使用 WebClient 和 WebRequest,我遇到了错误 "Please enable javascript" 并且内容没有加载,所以我使用了下面的代码,现在没有显示 javascipt 错误,但是网页内容没有加载。很长一段时间以来,我一直在为这个问题而苦苦挣扎,已经看到很多 post 与此有关,但无法完成这项工作。

有人可以帮忙吗?

提前致谢..

class Program
{
    private static bool completed = false;
    private static WebBrowser wb;
    [STAThread]
    static void Main(string[] args)
    {
        wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("http://justicecourts.maricopa.gov/findacase/casehistory.aspx");
        while (!completed)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }
        Console.Write("\n\nDone with it!\n\n");
        Console.ReadLine();


    }

    static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Console.WriteLine(wb.Document.Body.InnerHtml);
        completed = true;
    }

}

尝试使用 PhantomJs 基本上就像 运行 没有 window 的网络浏览器。 (无头)

如果您真的只是想将 URL 的内容转储到控制台,试试这个:

using(WebClient client = new WebClient()) {
   Console.WriteLine(client.DownloadString(url));
}

尝试添加更多等待。

static void Main(string[] args)
    {
        wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("http://justicecourts.maricopa.gov/findacase/casehistory.aspx");
        while (!completed)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }
       //wait even more
       for (int i = 0; i < 6; i++)
        {
            Application.DoEvents();
            Thread.Sleep(1000);
        }
        Console.Write("\n\nDone with it!\n\n");
        Console.ReadLine();


    }

否则你可以使用 EO 付费浏览器。但在您的情况下,trail 将起作用,因为它不是 GUI application.as 它在 GUI 中显示 trail 消息。

在 EO 中你可以说..

EOContorol.WebView.LoadUrlAndWait(URL);