你如何解析格式不好的HTML文件? [英] How do you parse a poorly formatted HTML file?

查看:100
本文介绍了你如何解析格式不好的HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将数据导入应用程序,我必须解析一系列网页。每种类型的网页都提供相同类型的数据。问题是每个页面的HTML不同,所以数据的位置不同。另一个问题是HTML代码格式不好,因此无法使用类似XML的解析器。



到目前为止,我能想到的最好的策略是为每种页面定义一个模板,如:



模板A:

 < html> 
...
< tr>< td>缺少td
< td>的表列另一个表格列< / td>< / tr>
< tr>< td> $ data_item_1 $< / td>
...
< / html>

模板B:

 < HTML> 
...
< ul>< li>另一个格式不正确的页面< li> $ data_item_1 $< / td>< / tr>
...
< / html>

这样我就只需要一个单独的解析器来处理所有的页面,模板,然后检索 $ data_item_1 $ $ data_item_2 $ 等等。但是,它会变成很多工作。你能想到任何更简单的解决方案吗?任何图书馆都可以提供帮助?

谢谢通过整理来获得有效页面的页面源代码。你可以找到整洁的这里
。 Tidy已经绑定了很多编程语言。完成之后,您可以使用您喜欢的解析器/内容提取技术。


I have to parse a series of web pages in order to import data into an application. Each type of web page provides the same kind of data. The problem is that the HTML of each page is different, so the location of the data varies. Another problem is that the HTML code is poorly formatted, making it impossible to use a XML-like parser.

So far, the best strategy I can think of, is to define a template for each kind of page, like:

Template A:

<html>
...
  <tr><td>Table column that is missing a td 
      <td> Another table column</td></tr>
  <tr><td>$data_item_1$</td>
...
</html>

Template B:

<html>
...
  <ul><li>Yet another poorly formatted page <li>$data_item_1$</td></tr>
...
</html>

This way I would only need one single parser for all the pages, that would compare each page with its template and retrieving the $data_item_1$, $data_item_2$, etc. Still, it is going to be a lot of work. Can you think of any simpler solution? Any library that can help?

Thanks

解决方案

You can pass the page's source through tidy to get a valid page. You can find tidy here . Tidy has bindings for a lot of programming languages. After you've done this, you can use your favorite parser/content extraction technique.

这篇关于你如何解析格式不好的HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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