2010-02-01から1ヶ月間の記事一覧

hpricot:An Hpricot Showcase:Hpricot Challenge:Checking for a Few Attributes

>> require "hpricot" => true >> doc = Hpricot('<a href="http://www.foo1.org/">foo1</a><a href="http://www.foo1.org/">foo2</a><form action="hoge1"></form><form action="hoge2"></form>') => #<Hpricot::Doc {elem <a href="http://www.foo1.org/"> "foo1" </a>} {elem <a href="http://www.foo1.org/"> "foo2" </a>

hpricot:An Hpricot Showcase:Hpricot Challenge:Does an Element Meet a Selector?

まんま >> require "hpricot" => true >> doc = Hpricot.parse(%{<div class='outer'><div class='inner'>text</div></div>}) => #<Hpricot::Doc {elem <div class="outer"> {elem <div class="inner"> "text" </div>} </div>}> >> doc.at('.outer') => {elem <div class="outer"> {elem <div class="inner"> "text" </div>} <…</div>

hpricot:An Hpricot Showcase:Hpricot Challenge:Searching Inner HTML

まんま >> require "hpricot" => true >> doc = Hpricot('<a href="http://www.somewebsite.com">Click Me!</a>') => #<Hpricot::Doc {elem <a href="http://www.somewebsite.com"> "Click Me!" </a>}> >> doc.search("a[text()*='Click']") => #<Hpricot::Elements[{elem <a href="http://www.somewebsite.com"> "Click Me!" …</hpricot::elements[{elem>

hpricot:An Hpricot Showcase:Hpricot Challenge:Excepting the First

まんま require "hpricot" X =<<EOS <body> <div class="test">one</div> <div class="test">two</div> <div class="test">three</div> </body> EOS doc = Hpricot(X) p (doc/"div.test:first-child)") p (doc/"div.test:not(:first-child)") (doc/"div.test:not(:first-child)").remove p doc で、 /usr/local/ruby-1.8.7/lib/r…

hpricot:An Hpricot Showcase:Hpricot Challenge:Selecting only Immediate Children

まんま require "hpricot" X =<<EOS <ul> <ul> <li>A</li> </ul> <li>B</li> <li>C</li> </ul> EOS doc = Hpricot(X) p doc.search("li") p doc.search("./li") p doc.search("/li") # => A, B p doc.search(">li") # => C で、 #<Hpricot::Elements[{elem <li> "A" </li>}, {elem <li> "B" </li>}, {elem <li> "C" </li>}]> #

hpricot:An Hpricot Showcase:Hpricot Challenge:Strip all HTML tags

まんま >> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2<a href="http://www.foo3.org/">foo3</a></p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" {elem <a href="http://www.foo3.org/"> "foo3" </a>} </p>}> >> doc.to_plain_text => "foo1\n\nfoo2foo3 [http://www.foo3.org/]" >> …

hpricot:An Hpricot Showcase:Hpricot Challenge:#containers

>> require "hpricot" => true >> doc = Hpricot('<p>foo1</p><p>foo2<a href="http://www.foo3.org/">foo3</a></p>') => #<Hpricot::Doc {elem <p> "foo1" </p>} {elem <p> "foo2" {elem <a href="http://www.foo3.org/"> "foo3" </a>} </p>}> >> doc.containers => [{elem <p> "foo1" </p>}, {elem <p> "foo2" {elem </p>

hpricot:An Hpricot Showcase:Hpricot Challenge:Extracting multiple children from a table

ほぼ、まんま require "hpricot" X =<<EOS <table> <tr> <td>...stuff I don't want...</td> </tr> <tr> <td> <table> ------------rows i want <tr> <td> <table> <tr> <td>Field 1</td> <td>Field 2</td> </tr> </table> </td> <td>Field 3</td> <td>Field 4, Field 5</td> </tr> ------------end of rows i want </table> </td></tr></eos>

hpricot:An Hpricot Showcase:Hpricot Fixups: :xhtml_strict

良く分かっていない >> require "hpricot" => true >> doc = Hpricot('<hoge></hoge>') => #<Hpricot::Doc {emptyelem <hoge>}> >> doc.to_html => "<hoge></hoge>" >> doc = Hpricot('<hoge></hoge>', :xhtml_strict => true) => #<Hpricot::Doc {elem <hoge> {bogusetag </hoge>}}> >> doc.to_html => "<hoge></hoge>" >> doc = Hp…

hpricot:An Hpricot Showcase:Hpricot Fixups: :fixup_tags

良く分かっていない >> require "hpricot" => true >> doc = Hpricot('', :fixup_tags => true) => #<Hpricot::Doc> >> doc.to_html => "" >> doc = Hpricot('<html></html>', :fixup_tags => true) => #<Hpricot::Doc {emptyelem <html>}> >> doc.to_html => "<html></html>" >> doc = Hpricot('<html>') => #</html></hpricot::doc></hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot XPath Search

>> require "hpricot" => true >> doc = Hpricot('<div><img src="http://www.foo.org/hoge1.png"><img src="http://www.bar.org/hoge2.png"></div><div><img src="http://www.foo.org/hoge3.png"></div><img src="http://www.foo.org/hoge4.png">') => #<Hpricot::Doc {elem <div> {emptyelem </hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Negating Searches

>> require "hpricot" => true >> doc = Hpricot('<html><body><p class="posted">A simple <b>test</b> string.</p><p>foo</p></body></html>') => #<Hpricot::Doc {elem <html> {elem <body> {elem <p class="posted"> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </body>} </html>}> >>…

hpricot:An Hpricot Showcase:Hpricot CSS Search:Searching Attributes

>> require "hpricot" => true >> doc = Hpricot('<input name="foo1" checked><input name="foo2"><input name="foo3" checked>') => #<Hpricot::Doc {emptyelem <input checked name="foo1">} {emptyelem <input name="foo2">} {emptyelem <input checked name="foo3">}> >> doc.search("input[@checked]") => #</hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Selecting Close Children

>> require "hpricot" => true >> doc = Hpricot('<div class="entryPermalink"><div class="child"><a href="http://www.foo1.org/">foo1</a></div><a href="http://www.foo2.org/">foo2</a></div>')=> #<Hpricot::Doc {elem <div class="entryPermalink"> {elem <div class="child"> {elem </div></hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Selecting by Hierarchy

>> require "hpricot" => true >> doc = Hpricot('<div class="entryPermalink"><a href="http://www.foo1.org/">foo1</a><a href="http://www.foo2.org/">foo2</a></div><div id="menu"><a href="http://www.foo3.org/">foo3</a></div><div class="entryPermalink"><a href="http://www.foo4.org/">foo4</a></div>') => #<…

hpricot:An Hpricot Showcase:Hpricot CSS Search:Selecting by Class

>> require "hpricot" => true >> doc = Hpricot('<p class="entryTitle"></p><div id="menu"></div>') => #<Hpricot::Doc {emptyelem <p class="entryTitle">} {emptyelem <div id="menu">}> >> doc.search("entryTitle") => #<Hpricot::Elements[]> >> doc.search(".entryTitle") => #</hpricot::elements[]></div></hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Selecting by Tag Name

>> require "hpricot" => true >> doc = Hpricot('<div id="menu"></div><div id="menu"></div>') => #<Hpricot::Doc {emptyelem <div id="menu">} {emptyelem <div id="menu">}> >> doc.search("div") => #<Hpricot::Elements[{emptyelem <div id="menu">}, {emptyelem <div id="menu">}]> >> doc.search("div").length =>…</div></hpricot::elements[{emptyelem></div></hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Selecting by ID

>> require "hpricot" => true >> doc = Hpricot('<div id="menu"></div>') => #<Hpricot::Doc {emptyelem <div id="menu">}> >> doc.search('#menu') => #<Hpricot::Elements[{emptyelem <div id="menu">}]> >> doc = Hpricot('<div id="menu"></div><div id="menu"></div>') => #</hpricot::elements[{emptyelem></hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot CSS Search:Using CSS Selectors

>> require "hpricot" => true >> doc = Hpricot('<div><img src="http://www.foo.org/hoge1.png"><img src="http://www.bar.org/hoge2.png"><img src="http://www.foo.org/hoge3.png"></div>') => #<Hpricot::Doc {elem <div> {emptyelem <img src="http://www.foo.org/hoge1.png">} {emptyelem </hpricot::doc>

hpricot:An Hpricot Showcase:Hpricot Altering:Altering Elements

まんま >> require "hpricot" => true >> doc = Hpricot("That's my <b>spoon</b>, Tyler.") => #<Hpricot::Doc "That's my " {elem <b> "spoon" </b>} ", Tyler."> >> doc.at("b").swap("<i>fork</i>") => [{elem <i> "fork" </i>}] >> doc.to_html => "That's my <i>fork</i>, Tyler." >> doc.at("i").swap { em "grapefruit…

hpricot:An Hpricot Showcase:Hpricot Elements:wrap

>> require "hpricot" => true >> doc = Hpricot('<a href="http://www.foo.org/">foo</a>') => #<Hpricot::Doc {elem <a href="http://www.foo.org/"> "foo" </a>}> >> doc.search("a[@href]").wrap(%{<div class="link"><div class="link_inner"></div></div>}) => #

hpricot:An Hpricot Showcase:Hpricot Elements:prepend

>> require "hpricot" => true >> doc = Hpricot("<p>foo</p><p>bar</p>") => #<Hpricot::Doc {elem <p> "foo" </p>} {elem <p> "bar" </p>}> >> doc.search("p").prepend("hoge") => #<Hpricot::Elements[{elem <p> "hoge" "foo" </p>}, {elem <p> "hoge" "bar" </p>}]> >> doc = Hpricot('<a href="http://www.foo.org/">…</a>

hpricot:An Hpricot Showcase:Hpricot Elements:append

>> require "hpricot" => true >> doc = Hpricot("<p>foo</p><p>bar</p>") => #<Hpricot::Doc {elem <p> "foo" </p>} {elem <p> "bar" </p>}> >> doc.search("p").append(" &raquo; " + "hoge") => #<Hpricot::Elements[{elem <p> "foo" " &raquo; hoge" </p>}, {elem <p> "bar" " &raquo; hoge" </p>}]>

hpricot:An Hpricot Showcase:Hpricot Elements:search

>> require "hpricot" => true >> doc = Hpricot("<p>foo<b>bar</b></p><p>baz</p>") => #<Hpricot::Doc {elem <p> "foo" {elem <b> "bar" </b>} </p>} {elem <p> "baz" </p>}> >> doc.search("p").search("b") => #<Hpricot::Elements[{elem <b> "bar" </b>}]> >> doc.search("p").at("b") => {elem <b> "bar" </b>} >> do…

hpricot:An Hpricot Showcase:Hpricot Elements:at

>> require "hpricot" => true >> doc = Hpricot("<p>foo<b>bar</b></p><p>baz</p>") => #<Hpricot::Doc {elem <p> "foo" {elem <b> "bar" </b>} </p>} {elem <p> "baz" </p>}> >> doc.search("p").at("b") {|x| puts "*#{x}*"} *<b>bar</b>* => {elem <p> "foo" {elem <b> "bar" </b>} </p>} >> doc.at("p") {|x| puts "*#…

hpricot:An Hpricot Showcase:Hpricot Basics:Figuring Out Paths

>> require "hpricot" => true >> doc = Hpricot("<html><body><p>A simple <b>test</b> string.</p><p>foo</p></body></html>") => #<Hpricot::Doc {elem <html> {elem <body> {elem <p> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </body>} </html>}> >> doc.at("p/b") => {elem <b> "t…</b>

hpricot:An Hpricot Showcase:Hpricot Basics:Looping Edits

>> require "hpricot" => true >> doc = Hpricot("<html><body><p class='posted'>A simple <b>test</b> string.</p><p>foo</p></body></html>") => #<Hpricot::Doc {elem <html> {elem <body> {elem <p class="posted"> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </body>} </html>}> >>…

hpricot:An Hpricot Showcase:Hpricot Basics:Continuing Searches

>> require "hpricot" => true >> doc = Hpricot("<html><p>A simple <b>test</b> string.</p><p>foo</p></html>") => #<Hpricot::Doc {elem <html> {elem <p> "A simple " {elem <b> "test" </b>} " string." </p>} {elem <p> "foo" </p>} </html>}> >> doc.search("/html/body//p") => #<Hpricot::Elements[]> >> doc = H…</hpricot::elements[]>