html特征字符串crystal,Crystal的一个Html解析器(类似于Ruby的Nokogiri)

Crystagiri

An HTML parser library for Crystal like the amazing Nokogiri Ruby gem.

I won't pretend that Crystagiri does much as Nokogiri. All help is welcome! :)

Installation

Add this to your application's shard.yml:

dependencies:

crystagiri:

github: madeindjs/crystagiri

and then run

$ crystal deps

Usage

require "crystagiri"

Then you can simply instantiate a Crystagiri::HTML object from an HTML String like this

doc = Crystagiri::HTML.new "

Crystagiri is awesome!!

"

... or directly load it from a Web URL or a pathname:

doc = Crystagiri::HTML.from_file "README.md"

doc = Crystagiri::HTML.from_url "http://example.com/"

Then you can search all XML::Nodes from the Crystagiri::HTML instance. The tags found will be Crystagiri::Tag objects with the .node property:

CSS query

puts doc.css("li > strong.title") { |tag| puts tag.node}

# => ..

# => ..

Known limitations: Currently, you can't use CSS queries with complex search specifiers like :nth-child

HTML tag

doc.where_tag("h2") { |tag| puts tag.content }

# => Development

# => Contributing

HTML id

puts doc.at_id("main-content").tagname

# => div

HTML class attribute

doc.where_class("summary") { |tag| puts tag.node }

# =>

..

# =>

..

# =>

..

Benchmark

I know you love benchmarks between Ruby & Crystal, so here's one:

require "nokogiri"

t1 = Time.now

doc = Nokogiri::HTML File.read("spec/fixture/HTML.html")

1..100000.times do

doc.at_css("h1")

doc.css(".step-title"){ |tag| tag }

end

puts "executed in #{Time.now - t1} milliseconds"

executed in 00:00:15.19 seconds with Ruby 2.4.1 with RVM

require "crystagiri"

t = Time.now

doc = Crystagiri::HTML.from_file "./spec/fixture/HTML.html"

1..100000.times do

doc.at_css("h1")

doc.css(".step-title") { |tag| tag }

end

puts "executed in #{Time.now - t} milliseconds"

executed in 00:00:04.40 seconds on Crystal 0.24.2 on LLVM 4.0.0 with release flag

Crystagiri is three time faster than Nokogiri!!

Development

Clone this repository and navigate to it:

$ git clone https://github.com/madeindjs/crystagiri.git

$ cd crystagiri

You can generate all documentation with

$ crystal doc

And run spec tests to ensure everything works correctly

$ crystal spec

Contributing

Create your feature branch git checkout -b my-new-feature

Commit your changes git commit -am "Add some feature"

Push to the branch git push origin my-new-feature

Create a new Pull Request

Contributors

madeindjs - creator, maintainer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值