当然調べます。
ってなわけでリファレンスの出番ですよ先生。
http://tam.qmix.org/wiki/Hpricot.html
Route 477
inner_html
inner_textと違って指定した要素の中のHTMLも一緒に抜き出す。
doc = Hpricot(open("http://newsing.jp")) (doc/"a.medium").inner_html.each do |text| puts text.tosjis end
出力結果
コメントを見る (1)<img src="/common/images/icn/icn_edit02.gif" border="0" height="18" alt="コメント" align="absmiddle" width="20" /> コメントをする (中略) コメントを見る (2)<img src="/common/images/icn/icn_edit02.gif" border="0" height="18" alt="コメント" align="absmiddle" width="20" /> コメントをする
img要素も一緒に抜き出すことができました。けどこれは俺の望んだものじゃない。
to_html
doc = Hpricot(open("http://newsing.jp")) (doc/"a.medium").to_html.each do |text| puts text.tosjis end
出力結果
<a href="http://newsing.jp/entry?url=allabout.co.jp%2Fgs%2Fgoodsleep%2Fcloseup%2FCU20070519A%2Findex.htm" class="medium"> コメントを見る (1)</a><a href="http://newsing.jp/comment?url=http%3A%2F%2Fallabout.co.jp%2Fgs%2Fgoodsleep%2Fcloseup%2FCU20070519A%2Findex.htm&w=open" class="medium"><img src="/common/images/icn/icn_edit02.gif" border="0" height="18" alt="コメント" align="absmiddle" width="20" /> コメントをする</a><a href="http://newsing.jp/entry?url=www.boraro.gozaru.jp%2Fdiary%2Fsonota%2Fsonota-entry-008.html" class="medium"> (中略) コメントを見る (2)</a><a href="http://newsing.jp/comment?url=http%3A%2F%2Fallabout.co.jp%2Frelationship%2Fwomenlove%2Fcloseup%2FCU20070513A%2F&w=open" class="medium"><img src="/common/images/icn/icn_edit02.gif" border="0" height="18" alt="コメント" align="absmiddle" width="20" /> コメントをする</a>
これだよこれ!このto_htmlというもの、Hpricot::Elemに対して有効なメソッドなので、doc.to_htmlとかで最初からHTML文章として扱うこともできます。かといって下記のように
doc = Hpricot(open("http://newsing.jp")).to_html (doc/"a.medium").each do |text| puts text.tosjis end
としてしまうと、XPATH,CSSでの検索ができなくなってしまうので注意。
取り出し系のメソッドはこの3つだけみたい。意外と少なくて残念。取り出した後の加工は自分でしろって感じなのかな。