python binding for parsley
Go to file
Kyle Maxwell 712f3a64e3 tentative rename, etc 2009-03-03 21:31:24 -08:00
python tentative rename, etc 2009-03-03 21:31:24 -08:00
ruby tentative rename, etc 2009-03-03 21:31:24 -08:00
test tentative rename, etc 2009-03-03 21:31:24 -08:00
.gitignore failing tests 2009-02-26 12:17:27 -08:00
AUTHORS
ChangeLog
INSTALL tentative rename, etc 2009-03-03 21:31:24 -08:00
INTRO tentative rename, etc 2009-03-03 21:31:24 -08:00
Makefile.am tentative rename, etc 2009-03-03 21:31:24 -08:00
Makefile.in tentative rename, etc 2009-03-03 21:31:24 -08:00
NEWS
PAPER tentative rename, etc 2009-03-03 21:31:24 -08:00
Portfile tentative rename, etc 2009-03-03 21:31:24 -08:00
Portfile.in tentative rename, etc 2009-03-03 21:31:24 -08:00
README.C-LANG tentative rename, etc 2009-03-03 21:31:24 -08:00
README.markdown tentative rename, etc 2009-03-03 21:31:24 -08:00
TODO tentative rename, etc 2009-03-03 21:31:24 -08:00
VERSION
aclocal.m4 added bisect to Makefile 2009-02-23 14:19:48 -08:00
bootstrap.sh
config.guess
config.status tests, misc 2009-02-25 12:00:43 -08:00
config.sub
configure added bisect to Makefile 2009-02-23 14:19:48 -08:00
configure.ac rename misc 2009-03-03 20:55:21 -08:00
depcomp
functions.c tentative rename, etc 2009-03-03 21:31:24 -08:00
functions.h tentative rename, etc 2009-03-03 21:31:24 -08:00
install-sh
kstring.c tentative rename, etc 2009-03-03 21:31:24 -08:00
kstring.h
libtool install-all task 2009-02-28 11:40:42 -08:00
ltmain.sh local ltmain.sh 2009-02-23 16:50:00 -08:00
missing
obstack.c
obstack.h
parser.y tentative rename, etc 2009-03-03 21:31:24 -08:00
parsley.c tentative rename, etc 2009-03-03 21:31:24 -08:00
parsley.h tentative rename, etc 2009-03-03 21:31:24 -08:00
parsley_main.c tentative rename, etc 2009-03-03 21:31:24 -08:00
parsley_mem.c tentative rename, etc 2009-03-03 21:31:24 -08:00
parsley_mem.h tentative rename, etc 2009-03-03 21:31:24 -08:00
parsleyc_main.c tentative rename, etc 2009-03-03 21:31:24 -08:00
printbuf.c
printbuf.h
regexp.c optional flags for dexter yay\! 2009-02-26 15:17:33 -08:00
scanner.l
util.c tentative rename, etc 2009-03-03 21:31:24 -08:00
util.h tentative rename, etc 2009-03-03 21:31:24 -08:00
xml2json.c tentative rename, etc 2009-03-03 21:31:24 -08:00
xml2json.h
y.tab.h include y.tab.h 2009-02-23 16:52:34 -08:00
ylwrap

README.markdown

Overview 

parsley is a simple language for data-extraction from XML-like documents (including HTML). parsley is:

  1. Blazing fast -- Typical HTML parses are sub-50ms.
  2. Easy to write and understand -- parsley uses your current knowledge of JSON, CSS, and XPath.
  3. Powerful. parsley can understand full XPath, including standard and user-defined functions.

Examples

A simple script, or "parslet", looks like this:

{
  "title": "h1",
  "links(a)": [
    {
      "text": ".",
      "href": "@href"
    }
  ]
}

This returns JSON or XML output with the same structure. Applying this parslet to http://www.yelp.com/biz/amnesia-san-francisco yields either:

{
  "title": "Amnesia",
  "links": [
    {
      "href": "\/",
      "text": "Yelp"
    },
    {
      "href": "\/",
      "text": "Welcome"
    },
    {
      "href": "\/signup?return_url=%2Fuser_details",
      "text": " About Me"
    },
    .....
  ]
}

or equivalently:

<parsley:root>
  <title>Amnesia</title>
  <links>
    <parsley:group>
      <href>/</href>
      <text>Yelp</text>
    </parsley:group>
    <parsley:group>
      <href>/</href>
      <text>Welcome</text>
    </parsley:group>
    <parsley:group>
      <href>/signup?return_url=%2Fuser_details</href>
      <text> About Me</text>
    </parsley:group>
    .....
  </links>
</parsley:root>      

This parslet could also have been expressed as:

{
  "title": "h1",
  "links(a)": [
    {
      "text": ".",
      "href": "@href"
    }
  ]
}

The "a" in links(a) is a "key selector" -- an explicit grouping (with scope) for the array. You can use any XPath 1.0 or CSS3 expression as a value or a key selector. Parsley will try to be smart, and figure out which you are using. You can use CSS selectors inside XPath functions -- "substring-after(h1>a, ':')" is a valid expression.

Variables

You can use $foo to access the value of the key "foo" in the current scope (i.e. nested curly brace depth). Also available are $parent.foo, $parent.parent.foo, $root.foo, $root.foo.bar, etc.

Custom Functions

You can write custom functions in XSLT (I'd like to also support C and JavaScript). They look like:

<func:function name="user:excited">
   <xsl:param name="input" />
   <func:result select="concat($input, '!!!!!!!')" />
</func:function>

If you run:

{
  "title": "user:excited(h1)",
}

on the Yelp page, you'll get:

{
  "title": "Amnesia!!!!!!!",
}