Hpricot

A Fast, Enjoyable HTML Parser for Ruby

[url]http://code.whytheluckystiff.net/hpricot/[/url]-->[url]http://wiki.github.com/why/hpricot[/url]


#!ruby
doc.search("//p[@class='posted']")
#=> #<Hpricot:Elements[{p ...}, {p ...}]>

#!ruby
(doc/"p.posted")
#=> #<Hpricot:Elements[{p ...}, {p ...}]>


查找所有p元素 并且 class 为 posted的元素

#!ruby
doc.at("body")['onload']
返回第一个元素,最常见的愿意是直接使用元素,读写元素属性。

#!ruby
(doc/"#elementID").inner_html
#=> "..<b>contents</b>.."
获得元素内容。

#!ruby
(doc/"#elementID").first.inner_html
#=> "..<b>contents</b>.."


如果匹配很多,可以是用.first 访问第一个元素。


#!ruby
(doc/"#elementID").to_html
#=> "<div id='elementID'>...</div>"
获得整个元素,包括外面的元素。


#!ruby
(doc/"p/a/img").each do |img|
puts img.attributes['class']
end
所有的搜索返回的都是元素集合。可以循环遍历。

#!ruby
doc.search("div.entryPermalink").at("a")
返回class为entryPermalink 第一个div,第一个a元素


#!ruby
# find all paragraphs.
elements = doc.search("/html/body//p")
# continue the search by finding any images within those paragraphs.
(elements/"img")
#=> #<Hpricot::Elements[{img ...}, {img ...}]>


继续搜索。

#!ruby
# the xpath version XPATH方式
(doc/"/html/body//p//img")
# the css version CSS方式
(doc/"html > body > p img")
# ..or symbols work, too! 标记方式
(doc/:html/:body/:p/:img)


#!ruby
(doc/"span.entryPermalink").each do |span|
span.attributes['class'] = 'newLinks'
end
puts doc


循环编辑

#!ruby
doc.search("div.entryPermalink").search("a") do |link|
pp link
end.search("span") do |span|
pp span
end


搜索所有 class 为 entryPermalink的div元素中的a 元素。遍历
然后搜索所有 class 为 entryPermalink的div元素 中的span元素,遍历 打印。


Using CSS Selectors CSS选择器
#!ruby
doc = Hpricot(open("qwantz.html"))
(doc/'div img[@src^="http://www.qwantz.com/comics/"]')
#=> Elements[...]


找到div里面img 元素中 src属性为http://www.qwantz.com/comics/的元素。

#!ruby
puts doc.search('#menu').inner_html


找到id为menu的元素,显示内部数据

#!ruby
puts doc.search("span").length


得到span类型的元素,length显示了数组的长度

#!ruby
puts (doc/:span).length


找到span元素,与上面的作用相同

Selecting by Class
#!ruby
doc.search(".entryTitle").each do |title|
puts title.inner_html
end


找到class属性为entryTitle的所有元素

#!ruby
(doc/"div.entryTitle").remove



XPATH也可以是实现类似的目标,//div[@class='entryTitle']
但是css方式似乎更出众。例如一个元素有多个class属性,那XPATH就无能为力了。

<div class="entryTitle dark">

#!ruby
(doc/"div[@class~='entryTitle']").remove



#!ruby
(doc/"div.entryPermalink a").empty
找到所有的class 为 entryPermalink 的div元素 下的 a元素

如果用css选择器栈方式来实现是
#!ruby
(doc/"div.entryPermalink"/"a").empty



#!ruby
doc.search("div.entryPermalink > a").
prepend("<b>found you on the left</b>").
append("<b>found you on the right</b>")


如果希望搜索的对象是 直接子元素,需要 > 符号。


#!ruby
doc.search("input[@checked]")


搜索input字段type为checked

#!ruby
doc.at("a[@name='part_two']")


搜索name属性为part_two的 a 对象


#!ruby
doc.search("*[@onclick*='document.location']").each do |ele|
ele.remove_attribute('onclick')
end
搜索所有onclick属性为document.location的对象。

#!ruby
doc.search("p:not(.blue)")


非选择符。P元素class属性不是blue的元素集合


Searching Hpricot With XPath

#!ruby
doc = Hpricot(URI.parse("http://we-make-money-not-art.com/").read)
(doc/'//div/img')
#=> Elements[...]


目前有些限制。
/ 开头的搜索可以使用xpath方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值