簡體   English   中英

使用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錯誤,但現在未顯示錯誤,但網頁內容未加載。 我長期以來一直在為這個問題而苦苦掙扎,已經看到很多有關此問題的帖子,而無法完成這項工作。

誰能幫忙嗎?

提前致謝..

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;
    }

}

如果您只是想將該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瀏覽器付費。 但是在您的情況下,跟蹤將起作用,因為它不是GUI應用程序。因為它在GUI中顯示跟蹤消息。

在EO中你可以說..

EOContorol.WebView.LoadUrlAndWait(URL);

基本上像在沒有窗口的情況下運行Web瀏覽器一樣,嘗試使用PhantomJs (無頭)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM