Git Product home page Git Product logo

htmlreader's Introduction

HTMLReader

A WHATWG-compliant HTML parser with CSS selectors in Objective-C and Foundation. It parses HTML just like a browser.

Usage

#import <HTMLReader/HTMLReader.h>

NSString *html = @"<p><b>Ahoy there sailor!</b></p>";
HTMLDocument *document = [HTMLDocument documentWithString:html];
NSLog(@"%@", [document firstNodeMatchingSelector:@"b"].textContent); // => Ahoy there sailor!

Installation

You have choices:

  • Copy the files in the Code folder into your project.

  • Add the following line to your Podfile:

    pod "HTMLReader"

  • Clone this repository (perhaps add it as a submodule), add HTMLReader.xcodeproj to your project/workspace, and add libHTMLReader.a to your iOS target or HTMLReader.framework to your OS X target.

HTMLReader has no dependencies other than Foundation.

Why HTMLReader?

I needed to scrape HTML like a browser. I couldn't find a good choice for iOS.

The Alternatives

libxml2 ships with iOS. It parses a variant of HTML 4 and does not handle broken markup like a modern browser.

Other Objective-C libraries I came across (e.g. hpple and Ono) use libxml2 and inherit its shortcomings.

There are C libraries such as Gumbo or Hubbub, but you need to shuffle data to and from Objective-C.

WebKit ships with iOS, but its HTML parsing abilities are considered private API. I consider a round-trip through UIWebView inappropriate for parsing HTML. And I didn't make it very far into building my own copy of WebCore.

Google Toolbox for Mac will escape and unescape strings for HTML (e.g. &amp; โ‡” &) but, again, not like a modern browser. For example, GTM will not unescape &#65 (note the missing semicolon).

CFStringTransform does numeric entities via (the reversible) kCFStringTransformToXMLHex, but that rules out named entities.

Does it work?

HTMLReader continually runs html5lib's tokenization and tree construction tests, ignoring the tests for <template> (which HTMLReader does not implement).

HTMLReader is continually tested on iOS 7 and OS X versions 10.8 and 10.9. It is continually built (but not tested, due to XCTest's availability) on iOS 6. It should work on iOS 5 but no automated testing is done.

Given all that: Build Status

HTMLReader is used by at least one shipping app.

How fast is it?

I'm not sure.

Included in the project is a utility called Benchmarker. It knows how to run three tests:

  • Parsing a large HTML file. In this case, the 7MB single-page HTML specification.
  • Escaping and unescaping entities in the large HTML file.
  • Running a bunch of CSS selectors. Basically copied from a WebKit performance test.

Changes to HTMLReader should not cause these benchmarks to run slower. Ideally changes make them run faster!

Bugs and Feature Requests

Bugs can be reported, and features can be requested, using the issue tracker. Or get in touch directly if you'd prefer.

License

HTMLReader is in the public domain.

Acknowledgements

HTMLReader is developed by Nolan Waite.

Thanks to Chris Williams for contributing the implementation of CSS selectors.

htmlreader's People

Contributors

nolanw avatar ultramiraculous avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.