Parsing web pages

耗尽温柔 提交于 2020-01-21 05:50:47

问题


I have a question about parsing HTML pages, specificaly forums, i want to parse a forum or thread containing certain post criterias, i havent defined the algorithm yet, since i have only parsed structure text formats before, A use case may be copy and paste each thread into the program by hand, or insert a URL like http://www.forums.com/forum/showthread.php?t=46875&page=3 and let the program parse the pages

Given all this i would like to know:

  1. Is it possible to parse a forum thread on a HTML page?
  2. what would be the best/Fastest/easiest language for doing this?
  3. If i prefer Java what tools/libraries do i need for this?
  4. Any other thing i should consider?

回答1:


1 / yes

2 / Use some compact language like python or ruby for prototyping.

  • For python there is a neat library for HTML/XML parsing called beautifulsoup

  • For ruby, you could try: nokogiri or hpricot

3 / A Java tool to consider: htmlparser

4 / If you are interested only in some particular text or some special classes, a regular expression might be sufficient. But as soon as you want to dig deeper into the structure of the content, you'll need some kind of model to hold your data, and hence a parser, which, in the best case, can cope with the occuring incosistencies of real world html.




回答2:


You might want to look into some sort of html parsing library, rather than using regular expressions to do this. There are some really good html parsers for ruby and python, but a quick google shows there to be a number of parsers for java as well. The benefit of these libraries is that you don't have to handle every edge case with regular expressions/they handle malformed html (both of which can be impossible with regexes, depending on what you want to do) and they also give you a much way of dealing with the data (for example, beautiful soup lets you grab all elements which belong to a specific class or to use some other css selector to limit which page elements you want to deal with).

Personally, I would, at least for the beginning, start in ruby or python, as the libraries are known and there is a lot of info about using them for this purpose. Also, I find it easier to quickly prototype these types of things in ruby or python than in the jvm. You could even later bring that code onto the jvm with jruby or jython, if it becomes necessary.




回答3:


  1. yes
  2. regular expressions, any flavor.
  3. probably the ones w/regex
  4. there are tools out there that will do this for you.


来源:https://stackoverflow.com/questions/1786689/parsing-web-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!