Giter Club home page Giter Club logo

lexbor-1's Introduction

Lexbor

Build Status

Fast HTML5 Parser with CSS selectors (based on new 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

Add this to your application's shard.yml:

dependencies:
  lexbor:
    github: kostya/lexbor

And run shards install (Installation require cmake, make sure it installed)

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
make
crystal spec

Benchmark

Parse google results page(600Kb) 1000 times, and 5000 times css select.

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

Running on Ryzen 3800x.

Lang Lib Parse time, s Css time, s Memory, MiB
Crystal lexbor 2.48 0.48 8.8
Crystal myhtml(+modest) 3.13 0.77 11.7
Ruby 2.7 Nokogiri(libxml2) 9.44 54.70 148.7
Crystal Crystagiri(libxml2) 11.90 - 25.1

Running on Apple M1.

Lang Lib Parse time, s Css time, s Memory, MiB
Crystal lexbor 1.54 0.49 20.9
Crystal myhtml(+modest) 2.24 0.67 19.8
Ruby 2.7 Nokogiri(libxml2) 9.87 55.72 163.8
Crystal Crystagiri(libxml2) 11.15 - 17.4

lexbor-1's People

Contributors

kostya avatar dependabot[bot] avatar mamantoha avatar hovsater avatar crimson-knight 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.