如何使用WPF WebBrowser组件来读取网页元素含量 [英] How to read element content from web page using the WPF WebBrowser component

查看:495
本文介绍了如何使用WPF WebBrowser组件来读取网页元素含量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得在C#中的网页元素值,与WPF WebBrowser组件?

How can I get the element value from the web page in C#, with the WPF WebBrowser component?

例如我想从这个网页<一个得到这个值1.7655 href="http://www.forexpros.com/currencies/usd-gel">http://www.forexpros.com/currencies/usd-gel.

感谢

推荐答案

在调用的导航方法=htt​​p://msdn.microsoft .COM / EN-US /库/ system.windows.controls.webbrowser.aspx相对=nofollow> web浏览器 WPF的成分 打开一个网页时, DocumentCompleted 事件到来时,你可以安全地浏览网页的内容(注意,有时该事件发生多次)。 web浏览器文件属性包含在已处理的格式HTML,叫DOM树。不幸的是,你不能使用这个属性很容易,因为它只是一个对象:此功能尚未在WPF(2011年12月)完成。

After you call the Navigate method of the WebBrowser component of WPF to open a webpage, the DocumentCompleted event arrives, and you can safely browse the content of the page (note that sometimes this event occurs multiple times). The Document property of WebBrowser contains the HTML in an already processed format, called the DOM tree. Unfortunately, you cannot use this property easily, since it is only an object. This feature has not been completed in WPF (December 2011).

我会使用 的WinForms版本 web浏览器 代替。如果你将它嵌入到一个<一个,你可以用它在WPF应用程序href="http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx"相对=nofollow> 的WindowsFormsHost 。这个类是完整的:它的文件属性是的HTMLDocument 对象,用车身属性,这是一个 的HtmlElement ,它包含了网页的内容。您可以步行DOM树递归地找到你想要的元素(和读取它的的InnerText ),或简单地处理使用正则表达式或的 HTML解析器库

I would use the Winforms version of WebBrowser instead. You can use it in a WPF application if you embed it into a WindowsFormsHost. This class is complete: its Document property is an HtmlDocument object, with a Body property, which is an HtmlElement, which contains the content of the page. You can walk the DOM tree recursively to find the element you want (and read its InnerText), or simply process the text of the whole page using Regex or an HTML parser library.

这篇关于如何使用WPF WebBrowser组件来读取网页元素含量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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