webBrowser1 获取html
时间: 2023-09-03 18:07:40 浏览: 64
你可以使用 WebBrowser 控件的 Document 属性来获取当前加载的网页文档对象。然后,你可以使用 Document 的 InnerHtml 属性来获取该网页的 HTML 代码。以下是一个示例代码:
```csharp
string htmlCode = "";
if (webBrowser1.Document != null)
{
htmlCode = webBrowser1.Document.Body.InnerHtml;
}
```
在这个示例中,我们首先检查 Document 属性是否为 null,以确保当前有一个网页已经加载到 WebBrowser 控件中。然后,我们使用 InnerHtml 属性获取网页的 HTML 代码,并将其保存在一个字符串变量中。
相关问题
WebBrowser 获取完整html
如果你需要获取完整的 HTML 代码,包括头部信息和 body 标签内的内容,可以使用以下代码:
```csharp
string htmlCode = "";
if (webBrowser1.Document != null)
{
HtmlElementCollection headElements = webBrowser1.Document.GetElementsByTagName("head");
HtmlElementCollection bodyElements = webBrowser1.Document.GetElementsByTagName("body");
string headHtml = "";
if (headElements.Count > 0)
{
headHtml = headElements[0].OuterHtml;
}
string bodyHtml = "";
if (bodyElements.Count > 0)
{
bodyHtml = bodyElements[0].OuterHtml;
}
htmlCode = "<!DOCTYPE html>\n<html>\n" + headHtml + "\n" + bodyHtml + "\n</html>";
}
```
在这个示例中,我们首先通过 GetElementsByTagName 方法获取 head 和 body 元素的集合。然后,我们分别获取这两个元素的 OuterHtml 属性,即包含标签本身的 HTML 代码。最后,我们将完整的 HTML 代码构建出来,包括 DOCTYPE 声明、head 和 body 元素。如果要获取 head 和 body 元素之外的其他元素的 HTML 代码,可以使用 Document.All 属性来获取所有的元素集合,然后遍历其中的元素,获取其 OuterHtml 属性。
vc WebBrowser获取html控件内容
要获取WebBrowser控件中的HTML内容,可以使用以下代码:
```c#
string html = webBrowser1.DocumentText;
```
这将返回WebBrowser控件中显示的HTML文本。如果要获取特定元素的HTML内容,可以使用以下代码:
```c#
HtmlElement element = webBrowser1.Document.GetElementById("elementId");
string html = element.InnerHtml;
```
此代码会获取具有给定ID的元素的HTML内容。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)