lexbor

Version, currently master branch1 version
  • master branchlatestMay 9, 2023

github.com/etra0/lexbor

Fast HTML5 Parser with CSS selectors. This is successor of myhtml and expected to be faster and use less memory.

0 stars
2 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  lexbor:
    github: etra0/lexbor
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
>= 0.35.1, < 2.0.0
License
MIT
Author
Konstantin Makarchev

Dependencies

This version declares no dependencies.

README

Lexbor

Build Status

Fast HTML5 Parser with CSS selectors (based on lexborisov's HTML5 parser lexbor). This is successor of myhtml and expected to be faster and use less memory. Usage is almost equal to myhtml.

Installation

Install dependency cmake:

sudo apt install cmake

Add this to your application's shard.yml:

dependencies:
  lexbor:
    github: kostya/lexbor

Usage example

require "lexbor"

html = <<-HTML
  <html>
    <body>
      <div id="t1" class="red">
        <a href="/#">O_o</a>
      </div>
      <div id="t2"></div>
    </body>
  </html>
HTML

lexbor = Lexbor::Parser.new(html)

lexbor.nodes(:div).each do |node|
  id = node["id"]?

  if first_link = node.scope.nodes(:a).first?
    href = first_link["href"]?
    link_text = first_link.inner_text

    puts "div with id #{id} have link [#{link_text}](#{href})"
  else
    puts "div with id #{id} have no links"
  end
end

# Output:
#   div with id t1 have link [O_o](/#)
#   div with id t2 have no links

Css selectors example

require "lexbor"

html = <<-HTML
  <html>
    <body>
      <table id="t1">
        <tr><td>Hello</td></tr>
      </table>
      <table id="t2">
        <tr><td>123</td><td>other</td></tr>
        <tr><td>foo</td><td>columns</td></tr>
        <tr><td>bar</td><td>are</td></tr>
        <tr><td>xyz</td><td>ignored</td></tr>
      </table>
    </body>
  </html>
HTML

lexbor = Lexbor::Parser.new(html)

p lexbor.css("#t2 tr td:first-child").map(&.inner_text).to_a
# => ["123", "foo", "bar", "xyz"]

p lexbor.css("#t2 tr td:first-child").map(&.to_html).to_a
# => ["<td>123</td>", "<td>foo</td>", "<td>bar</td>", "<td>xyz</td>"]

More Examples

examples

Development Setup:

git clone https://github.com/kostya/lexbor.git
cd lexbor
crystal src/ext/build_ext.cr
crystal spec

Benchmark

Parse google results page(1.5Mb) 1000 times, and 5000 times css select.

lexbor-program myhtml-program crystagiri-program nokogiri-program

Running on Ryzen 3800x.

LangLibParse time, sCss time, sMemory, MiB
Ruby 2.7Nokolexbor(lexbor)4.071.80112.5
Crystallexbor4.750.8012.3
Crystalmyhtml(+modest)5.981.2212.2
CrystalCrystagiri(libxml2)14.20-31.7
Ruby 2.7Nokogiri(libxml2)18.6792.69398.4

Running on Apple M1.

LangLibParse time, sCss time, sMemory, MiB
Crystallexbor2.770.6226.4
Ruby 2.7Nokolexbor(lexbor)3.411.11268.8
Ruby 2.7Nokogiri(libxml2)18.2287.21232.8
CrystalCrystagiri(libxml2)126.25-26.8