【问题标题】:Load web page contents in console application using c#使用 c# 在控制台应用程序中加载网页内容
【发布时间】:2015-12-02 13:10:32
【问题描述】:

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

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

通过使用 WebClient 和 WebRequest,我收到错误“请启用 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;
    }

}

【问题讨论】:

  • AFAIK WebClient 和 WebBrowser 都不支持 Javascript。使用 WebBrowser 控件会出现同样的错误

标签: javascript c# asp.net console-application asp.net-webpages


【解决方案1】:

如果您只是想将该 URL 的内容转储到控制台,请尝试以下操作:

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

【讨论】:

  • 完美!!它也是 exec javascript!
【解决方案2】:

尝试添加更多等待。

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

【讨论】:

  • 真棒@sm.abdullah,页面加载成功。非常感谢。
【解决方案3】:

尝试使用PhantomJs 基本上就像在没有窗口的情况下运行网络浏览器。 (无头)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2019-03-02
    相关资源
    最近更新 更多