繁体   English   中英

C#使用javascript解析html

[英]C# parsing html with javascript

在执行本文档中的javascript代码后,我需要解析html代码。 我使用webBrowser控件下载和控制html。

例如,我的html代码中有一些javascript。

<script type="text/javascript" src="http://site.com/script.js"></script>

感谢您的回答。

PS我的意思是:我必须解析所有代码,其中一些文本可以返回javascript。 因此,我只能在执行javascript之后解析文档。 因为我需要将某些动态内容添加到javascript中。

添加

我对javascript生成的内容感到满意。 我跳过了这一部分,因为我一直在寻找使用JavaScript生成的iframe中的某些内容。

现在我还有另一个问题。 在我的文档中,我没有几个iframe。 我正在尝试从某些框架中获取内容。 在下一种方式中:

        var htmlcol = webBrowser1.Document.Window.Frames;
        foreach (HtmlWindow item in htmlcol)
        {
            try
            {
                Console.Write(item.Name);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Something wrong");
            }

        }

但是以这种方式,我有一个例外:' System.UnauthorizedAccessException '。 我如何获得对HTML框架的访问?

PPS Sory我的英语不好:)

我认为,使用WebBrowserDocument属性表示的DOM会带来更好的体验。

您可以遍历Body的嵌套元素,或者使用GetElementByIdGetElementsByTagName查找所需的内容。

DOM应该通过JavaScript在页面中所做的更改自动更新。

请阅读您的问题的Phantomjs,并使用setTimeOut打开页面。

这可能是这样的:

var page = require('webpage').create();

page.open("https://sample.com", function(){
    page.evaluate(function(){
        // Execution somethings before page load. for Example: 
        localStorage.setItem("something", "whatever");// Set LocalStorage for browser before open
    });

    page.open("https://sample.com", function(){
        setTimeout(function(){
            console.log(page.content); //page source

            // Where you want to save it    
            page.render("screenshoot.png")  

            // You can access its content using jQuery
            var fbcomments = page.evaluate(function(){
                return $("body").contents().find(".content") 
            }) 

            phantom.exit();
        },10000)
    });    
});

请尝试以下操作:-将引用Microsoft.mshtml添加到您的应用程序。

尝试:

public void setPage(mshtml.HTMLWindow2Class JSFile)
{
HTMLWindow2Class window = new HTMLWindow2Class();
window = JSFile;

}
public void scriptPrint()
{
IHTMLDocument2 doc = null; ;
IHTMLWindow2 parentwindow = doc.parentWindow;



parentwindow.execScript("report_back('Printing complete!')", "JScript");
}

}

这也是一篇可能对您有帮助的文章: http : //www.dotnetcurry.com/ShowArticle.aspx?ID=194

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM