From: Robert Klemme Date: 2013-05-12T17:45:11+09:00 Subject: Re: parse both string and url using Nokogiri xpath --20cf306f750c8e4ab204dc816af8 Content-Type: text/plain; charset=ISO-8859-1 On Sun, May 12, 2013 at 1:37 AM, Soichi Ishida wrote: > ruby 1.9.3 > nokogiri 1.5.5 > > Say, a web page has a link, > > reference > > I would like to get both the url and text, "http://example.com" and > "reference". > > First, access to the page that contains this link. > > doc = Nokogiri::HTML(open(url)) > > then, > > name = doc.xpath('//div.../a').text > url = doc.xpath('//div.../a/@href).text > > It works. But the problem is this is parsing twice separately. > If you want to apply the same procedure to many links that exist in a > single page, it seems inefficient. > > Is there anyway to produce both url and text by single parse? like > > def parse_link_and_text (xpath) > ... > end > > p parse_link_and_text('//div...') > > gives a hash > > => {'reference' => 'http://example.com'} > > ? > Just search for and go from there. $ irb -r nokogiri irb(main):001:0> dom = Nokogiri.HTML('text') => #, #] children=[#]>]>]>]>]> irb(main):002:0> node = dom.at_xpath '//a' => #] children=[#]> irb(main):003:0> node[:href] => "link" irb(main):004:0> node.text => "text" irb(main):005:0> Now, what is so difficult about that? You can easily find out more via documentation. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --20cf306f750c8e4ab204dc816af8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Sun, May 12, 2013 at 1:37 AM, Soichi Ishida &l= t;lists@ruby-foru= m.com> wrote:
ruby 1.9.3
nokogiri 1.5.5

Say, a web page has a link,

=A0 <a href=3D"htt= p://example.com">reference</a>

I would like to get both the url and text, "http://example.com" and
"reference".

First, access to the page that contains this link.

doc =3D Nokogiri::HTML(open(url))

then,

name =3D doc.xpath('//div.../a').text
url =3D doc.xpath('//div.../a/@href).text

It works. =A0But the problem is this is parsing twice separately.
If you want to apply the same procedure to many links that exist in a
single page, it seems inefficient.

Is there anyway to produce both url and text by single parse? like

def parse_link_and_text (xpath)
=A0 =A0...
end

p parse_link_and_text('//div...')

gives a hash

=3D> {'reference' =3D> 'http://example.com'}

?
<= div class=3D"gmail_extra">
Just s= earch for <a> and go from there.

=
$ irb -r nokogiri
irb(main):001:0> dom =3D Nokogiri.HTML('<x><a href=3D&quo= t;link">text</a></x>')
=3D> #<Nokogiri::HTML::Document:0x434197c name=3D"document&q= uot; children=3D[#<Nokogiri::XML::DTD:0x43411d4 name=3D"html"&= gt;, #<Nokogiri::XML::Element:0x433df20 name=3D"html" children= =3D[#<Nokogiri::XML::Element:0x433daac name=3D"body" children= =3D[#<Nokogiri::XML::Element:0x433d48a name=3D"x" children=3D[= #<Nokogiri::XML::Element:0x433cfee name=3D"a" attributes=3D[#&= lt;Nokogiri::XML::Attr:0x433b086 name=3D"href" value=3D"link= ">] children=3D[#<Nokogiri::XML::Text:0x433be5a "text"= >]>]>]>]>]>
irb(main):002:0> node =3D dom.at_xpath '/= /a'
=3D> #<Nokogiri::XML::Element= :0x433cfee name=3D"a" attributes=3D[#<Nokogiri::XML::Attr:0x43= 3b086 name=3D"href" value=3D"link">] children=3D[#&l= t;Nokogiri::XML::Text:0x433be5a "text">]>
irb(main):003:0> node[:href]
=3D> "link"
i= rb(main):004:0> node.text
=3D> "= text"
irb(main):005:0>=A0

Now, what is so difficult about that? =A0You can easily find out more= via documentation.

Cheers

robert

--
remember.guy do |as,= often| as.you_can - without end
http://blog.rubybestpractices.com/
--20cf306f750c8e4ab204dc816af8--