将网站的一部分显示到Web浏览器中 [英] Display portion of a Website into a Web Browser

查看:112
本文介绍了将网站的一部分显示到Web浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导航到一个特定的网站,然后我希望只在网站的一部分中显示该网站,该网站的开头为:

i want to navigate to a specific website, and i want then to be displayed in the web browser only a portion of the website, which starts with:

<div id="dex1" ...... </div>

我知道我需要通过id获取元素,但首先我尝试编写以下代码:

I know i need to get the element by id, but firstly i tried writing this:

string data = webBorwser.Document.Body.OuterHtml;

因此,我需要从数据中获取该内容"id"并显示它,其余的要删除.

So from data i need to grab that content "id" and display it and the rest to be deleted.

对此有任何想法吗?

推荐答案

webBrowser1.DocumentCompleted += (sender, e) =>
{
    webBrowser1.DocumentText = webBrowser1.Document.GetElementById("dex1").OuterHtml;
};

再三考虑,不要这样做,设置DocumentText属性会导致再次触发DocumentCompleted事件.也许可以这样做:

On second thoughts, don't do that, setting the DocumentText property causes the DocumentCompleted event to fire again. So maybe do:

webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted;
    webBrowser1.DocumentText = webBrowser1.Document.GetElementById("dex1").OuterHtml;
}

尽管在大多数现实情况下,我希望您注入一些JavaScript来进行DOM操作时会得到更好的结果,这是安德烈(la Andrei)的回答.

Although in most real world cases I'd expect you'd get better results injecting some javascript to do the DOM manipulation, a la Andrei's answer.

仅替换body标签中的所有内容,如果幸运的话,可以维护所有必需的样式和脚本(如果它们全部位于头部)不引用任何丢弃的上下文,则您可能会感到高兴:

to just replace everything inside the body tag which might if you're lucky maintain all the required styling and scripts if they're all in the head don't reference any discarded context, you may have some joy with:

webBrowser1.Document.Body.InnerHtml = webBrowser1.Document.GetElementById("dex1").OuterHtml;

这篇关于将网站的一部分显示到Web浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆