Parse HTML more stably in groovy Mode

Source: Internet
Author: User

How to Use groovy to parse HTML code that cannot pass XML Verification

Original article: robust HTML parsing the groovy way

Parsing XML with groovy is simple, and it can run well as long as the input data format is good-but it is not always guaranteed in reality. Considering the HTML code, it is always difficult to verify them through XML, which requires tagsoup to save.

The main obstacle is:

  1. DTD
  2. Unclosed labels

Let's use a simple script to demonstrate parsing the stackoverflow page

[Java]View plaincopy

 
  1. Def slurper = new xmlslurper ()
  2. Def htmlparser = slurper. parse ("http://stackoverflow.com /")
  3. Htmlparser. '**'. findall {It. @ class = 'Question-hyperlink'}. Each {
  4. Println it
  5. }

The script accesses the homepage of stack overflow and prints all items with the 'Question-hyperlink' attribute. But the runtime throws the following exception: Caught: Java. Io. ioexception: server returned HTTP response code: 503 for URL: http://www.w3.org/TR/html4/strict.dtd at html_parser.run (html_parser.groovy: 7)

Note: This problem no longer exists after it is upgraded to groovy 1.8.

Xmlslurper encountered problems when parsing html dtd, which can be solved through another blog.

[Java]View plaincopy

 
  1. Def slurper = new xmlslurper ()
  2. Slurper. setfeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
  3. Def htmlparser = slurper. parse ("http://stackoverflow.com /")
  4. Htmlparser. '**'. findall {It. @ class = 'Question-hyperlink'}. Each {
  5. Println it
  6. }

However, the script reports an error again because of unclosed tags. Here, tagsoup is used to solve this problem. Best of all, tagsoup and xmlslurper can work very well. The following is an example:

 

[Java]View plaincopy

 
  1. @ Grab (group = 'org. ccil. Cowan. tagsoup ', module = 'tagsoup', version = '1. 2 ')
  2. Def tagsoupparser = new org. ccil. Cowan. tagsoup. Parser ()
  3. Def slurper = new xmlslurper (tagsoupparser)
  4. Def htmlparser = slurper. parse ("http://stackoverflow.com /")
  5. Htmlparser. '**'. findall {It. @ class = 'Question-hyperlink'}. Each {
  6. Println it
  7. }

The first line is used to obtain the tagsoup library, and then the tagsoupparser instance is assigned to xmlslurper.

This article Reprinted from http://blog.csdn.net/hiarcs/article/details/6628062

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.