简体   繁体   English

在C#中,使用WebKitBrowser,如何获取HTML?

[英]In C#, using the WebKitBrowser, how to get HTML?

I've been searching for answer for this all this morning and last night. 我今天早上和昨晚都在寻找这个问题的答案。

I use the WebKit.NET as an internal browser to mock the browser activity, and I followed the tutorial here . 我使用WebKit.NET作为内部浏览器来模拟浏览器活动,我在这里按照教程。

So when creating the form, I declare: 所以在创建表单时,我声明:

this.webKitBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webKitBrowser1_Navigated);

Where in the webKitBrowser1_Navigated event I try getting the document text using: 在webKitBrowser1_Navigated事件中,我尝试使用以下方式获取文档文本:

string content = webKitBrowser1.DocumentText;

But the content is empty . 内容是空的

Besides, I use webkitBrowser because I need the webkit engine to get the web-content. 此外,我使用webkitBrowser因为我需要webkit引擎来获取web内容。

Any idea on how can I get the text Content from WebKit Engine? 关于如何从WebKit Engine获取文本内容的任何想法? Thanks. 谢谢。

The content of the document (via the DocumentText property) is available when the web kit engine has finished loading the document. 当Web工具包引擎完成加载文档时,文档的内容(通过DocumentText属性)可用。 Accessing the property beforehand will result in an empty DocumentText property. 事先访问属性将导致DocumentText属性为空。

You should use the DocumentCompleted event to wait for the document to be loaded. 您应该使用DocumentCompleted事件等待加载文档。

The following code shows how to use the DocumentCompleted event: 以下代码显示了如何使用DocumentCompleted事件:

webKitBrowser1.Navigate("www.google.com");
webKitBrowser1.DocumentCompleted += 
     new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);

void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  string documentContent = webKitBrowser1.DocumentText;

  MessageBox.Show(documentContent);      
}

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

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