From: "Иван Бишевац" Date: 2012-09-12T23:11:40+09:00 Subject: Re: please help me with making script --f46d042e0255ecaf3604c981c4f1 Content-Type: text/plain; charset=UTF-8 2012/9/12 Charmaine Willemsen > In this example i like to parse birthday and sexe > > >
> > 6 apr 1961 >
>
> > Male >
> > ----------------------------------------------------------------------------- > > These ruby script will not work, please who can help me? > > options[:birthday] = find(:xpath, ".//span[contains(@id, > \"lblbirthday\")]").text > options[:sexe] = find(:xpath, ".//span[contains(@id, > \"lblsexe\")]").text > > -- > Posted via http://www.ruby-forum.com/. > > 1. Nokogiri doesn't have method find, maybe he is using Capybara. If so try to find with css since it's simpler: options[:birthday] = find("span#lblbirthday").text options[:sexe] = find("span#lblsexe").text In any case Capybara converts css to xpath when searching. 2. You could do it with Nokogiri too. Here is complete example: require 'nokogiri' html = "
6 apr 1961
Male
" doc = Nokogiri::HTML(html) options = {} options[:birthday] = doc.at_css('span#lblbirthday').content options[:sexe] = doc.at_css('span#lblsexe').content options.each { |key, value| puts "#{key}: #{value}" } It outputs: birthday: 6 apr 1961 sexe: Male --f46d042e0255ecaf3604c981c4f1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
2012/9/12 Charmaine Willemsen = <lists@ruby-fo= rum.com>
In this example i like to parse birthday and sexe


<div class=3D"floatleft">
=C2=A0 =C2=A0 =C2=A0 <label for=3D"lblbirthday" id=3D"lbl= birthdayLabel">birthday</label>
=C2=A0 =C2=A0 =C2=A0 <span id=3D"lblbirthday">6 apr 1961<= ;/span>
=C2=A0 =C2=A0 </div>
=C2=A0 =C2=A0 <div class=3D"floatleft" style=3D"margin-le= ft: 50px;">
=C2=A0 =C2=A0 =C2=A0 <label for=3D"lblsexe" id=3D"lblsexe= Label">sexe</label>
=C2=A0 =C2=A0 =C2=A0 <span id=3D"lblsexe">Male</span>=
=C2=A0 =C2=A0 </div>
---------------------------------------------------------------------------= --

These ruby script will not work, please who can help me?

options[:birthday] =C2=A0 =C2=A0 =C2=A0=3D find(:xpath, ".//span[conta= ins(@id,
\"lblbirthday\")]").text
options[:sexe] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=3D find(:xpath, ".//= span[contains(@id,
\"lblsexe\")]").text

--
Posted via http://= www.ruby-forum.com/.

1. Nokogiri doesn't have method find, = maybe he is using Capybara.
If so try to find with css since it's s= impler:
options[:birthday] =3D find("= ;span#lblbirthday").text
options[:sexe] =3D find("span#lblsexe&quo= t;).text

In any case Capybara converts css = to xpath when searching.



2. You could do it with Nokogiri too. Here is complete example:
<= div>
require 'nokogiri'

htm= l =3D "<div class=3D'floatleft'>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <label f= or=3D'lblbirthday' id=3D'lblbirthdayLabel'>birthday</= label>
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 <span id=3D'lblbirthday'>6 apr 1961</span>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 <div cla= ss=3D'floatleft' style=3D'margin-left: 50px;'>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <labe= l for=3D'lblsexe' id=3D'lblsexeLabel'>sexe</label>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <span id= =3D'lblsexe'>Male</span>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 </div>"

doc =3D = Nokogiri::HTML(html)
options =3D {}
options[:birthday] =3D doc.at_css('span#lblbirthday').con= tent
options[:sexe] =3D doc.at_css= ('span#lblsexe').content

= options.each { |key, value| puts "#{key}: #{value}" }

It outputs:
birthday: 6 apr 1961
sexe: Male
--f46d042e0255ecaf3604c981c4f1--