网页抓取 - 无法在网页中显示表格

时间:2012-12-12 21:06:38

标签: c# winforms web-scraping

我正在解析网页的HTML以获取一些信息。在我的网页中,我有<table>我正在尝试访问。但是当我编写以下代码时,返回0个元素:

WebBrowser csexBrowser = new WebBrowser();
HtmlElementCollection table2 = this.csexBrowser.Document.GetElementsByTagName("table");

这里,table2什么都没有。 0个元素。我正在使用winforms。

编辑:This is the link。如果您搜索名称,它会在表格中显示一些结果。

2 个答案:

答案 0 :(得分:0)

在访问您提供的链接之前有一个验证步骤。在http://www.nsopw.gov/en-US/Search/Verification文档中,没有表格。

您确定先通过了验证网址吗?

<强> [编辑]

请试试这个:

public Form1()
{
    InitializeComponent();
    WebBrowser csexBrowser = new WebBrowser();

    //here we say what we want to do when the Navigated event occurs
    csexBrowser.Navigated += csexBrowser_Navigated;

    //this takes some time
    csexBrowser.Navigate("http://www.nsopw.gov/en-US/Search");
}

void csexBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    //here the document is loaded and we will find the table
    HtmlElementCollection table2 = ((WebBrowser)sender).Document.GetElementsByTagName("table");
}

答案 1 :(得分:0)

如果您坚持使用浏览器导航,则必须等待导航完成。就个人而言,我讨厌这种方法加上大多数人去的事件火灾都可以找到我发现的多触发器。

这样做:

csexBrowser.Navigate(Url);
        while (csexBrowser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

只需导航到指定的网址,直到网页加载完毕后才会继续。 做完了。

相关问题