C# - 从javascript生成的webbrowser获取变量

时间:2012-06-28 10:17:55

标签: c# javascript browser

已通过webbrowser下载页面并需要获取邮件地址。但它是由javastript生成的。在代码中我可以找到这个脚本:

<script type="text/javascript" charset="utf-8">var i='&#109;a'+'i&#108;'+'&#116;o';var a='impexta&#64;impexta&#46;sk';document.write('<a href="'+i+':'+a+'" onclick="_kt.i(386728, 20);">'+a+'</a>');</script>

我到处都读到如何调用脚本,我不知道他的名字。所以我想要的是获得“变量”值。

编辑:之前的代码:

...
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate(url);

for (; wb.ReadyState != WebBrowserReadyState.Complete; )
{
    System.Windows.Forms.Application.DoEvents();
}
...

void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
        WebBrowser wb = sender as WebBrowser;
        if (wb != null)
        {
            if (wb.ReadyState == WebBrowserReadyState.Complete)
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.Load(wb.DocumentStream);
             }
        }
}

1 个答案:

答案 0 :(得分:1)

我找到了简单的解决方案。只需在HTML代码中找到正确的字符串部分:

foreach (HtmlNode link in root.SelectNodes("//script"))
{
    if (link.InnerText.Contains("+a+"))
    {
        string[] strs = new string[] { "var a='", "';document.write" };
        strs = link.InnerText.Split(strs, StringSplitOptions.None);
        outMail = System.Net.WebUtility.HtmlDecode(strs[1]);
        if (outMail != "")
        {
            break;
        }
    }
}