建议需要:解析HTML的在C#中的最佳方法 [英] Suggestion Needed: Best way of parsing HTML in C#

查看:114
本文介绍了建议需要:解析HTML的在C#中的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。这是提取从HTML页面的某些信息的最佳途径。
我目前做的是以下内容:




  1. 使用Web客户端下载页面


  2. 接收到的数据转换为使用UTF8Encoding


  3. 将字符串以XML字符串


  4. 从.NET Framework中使用XML相关的类提取所需的数据




这是我目前正在做总结的形式。任何人都知道另一种方法呢? ?一些可以更快,更容易。



最好的问候,
基里尔



PS:我有听说过所谓的华廷



<一个测试框架p >,允许你做同样的事情,但还没有研究它更


解决方案

这听起来像你已经找到了如何获取页面的数据(这是最简单的部分)。



有关休息,我用这种类型的任务的最佳托管库是的 HTML敏捷性包。它是开源的,非常成熟,完全在.NET编写的。它处理畸形的HTML和可以做你两种不同的方式所需要的:




  • 本机支持对XPath和XML类查询HTML DOM。它的目的是模仿.NET的XML库,所以什么可以做,对XML使用.NET,你可以对HTML与此有关。


  • 支持产生有效从HTML XML,所以你可以使用任何XML工具。



This is my question. Which is the best way to extract certain information from an HTML page. What I currently do is the following:

  1. Download the page using WebClient

  2. Convert the received data to string using UTF8Encoding

  3. Convert the string to XML

  4. Using Xml related classes from the .NET Framework extract the desired data

This is what I currently do in summarized form. Anyone aware of another method? Something that can be faster or easier?

Best Regards, Kiril

PS: I have heard about a testing framework called Watin

that allows you to do something similar, but haven't researched it much

解决方案

It sounds like you've figured out how to fetch the page data (that's the simplest part).

For the rest, the best managed library I've used for this type of task is the HTML Agility Pack. It's open source and very mature, written entirely in .NET. It handles malformed HTML and can do what you need in two different ways:

  • Natively supports XPATH and XML-like querying against the HTML DOM. It is designed to mimic .NET's XML library, so anything you can do against XML with .NET, you can do against HTML with this.

  • Supports producing valid XML from the HTML, so you can use any XML tools.

这篇关于建议需要:解析HTML的在C#中的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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