0

我想为 VK.com 编写一个页面解析器。我的问题是,页面源仅包含 50 个结果,其他结果在到达页面末尾后重新加载。

到目前为止我的代码:

    private void syncToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string[] information, title, artist;
        int i = 0;
        List<string> joint = new List<string>();
        information = info_basic(webBrowser1.DocumentText);
        title = info_title(information);
        artist = info_artist(information);
        foreach (string str in title)
        {
            joint.Add(artist[i] + " - " + title[i]);
            i++;
        }
        listBox1.Items.Clear();
        listBox1.Items.AddRange(joint.ToArray());
    }

    private string[] info_basic(string source)
    {
        string[] temps;
        List<string> sub = new List<string>();
        temps = Regex.Split(source, "<div class=\"play_btn fl_l\">");
        foreach (string str in temps)
        {
            sub.Add(str);
        }
        sub.RemoveRange(0, 1);
        return sub.ToArray();
    }

重要页面代码:

http://csharp.bplaced.net/files/vk%20source.txt

4

1 回答 1

0

我建议在滚动到底部时监视从页面到 vk.com 的流量(例如,使用 fiddler http 代理),并了解页面是如何动态加载的。这很可能是通过来自 javascript 的 ajax 异步调用来完成的。然后,在代码中模拟相同的行为以加载整个页面。HttpWebRequest 类最适合此任务。

但是由于您使用的是 webBrowser 控件,并且可能它完成了加载内容的所有工作 - 您可以尝试以编程方式滚动 Web 浏览器控件视图,以便本机 js 会触发并加载内容,当您到达底部时停止,并且然后解析整个加载的页面。

于 2013-03-22T21:38:17.900 回答