Hpricot html parsing

  • Thread starter Dhanasekaran Vivekanandhan
  • Start date
D

Dhanasekaran Vivekanandhan

hi all,
I have the following html fragment
I want to get the inner html content inside the
<p> <img></p> tag , not the between the <p> </p> tag.
for example in the following example i want to get the result as
"this is fun". I dont want to get the result including "NO FUN".
how to do with Hpricot

example html fragment:
----------------------

<p class=posted>
this is fun
<img src="" class="dhans"/>
</p>
<p class=posted>
NO FUN
</p>

thanks in advance,
dhanasekaran
 
P

Peter Szinek

Dhanasekaran said:
hi all,
I have the following html fragment
I want to get the inner html content inside the
<p> <img></p> tag , not the between the <p> </p> tag.
for example in the following example i want to get the result as
"this is fun". I dont want to get the result including "NO FUN".
how to do with Hpricot

example html fragment:
----------------------

<p class=posted>
this is fun
<img src="" class="dhans"/>
</p>
<p class=posted>
NO FUN
</p>

I did not quite get you. You want the text of the first <p> because it
has an image?
Or what is the exact criterion to accept/reject <p>'s?

Peter

__
http://www.rubyrailways.com
 
D

Dhanasekaran Vivekanandhan

yes, I want the text of the first <p> because it
has an image. and reject if <p> has no image.
thanks,
Dhanasekaran
 
L

lrlebron

You can try something like this:

if p.search("img").length > 0
puts p.inner_html
end
 
P

Peter Szinek

Dhanasekaran said:
yes, I want the text of the first <p> because it
has an image. and reject if <p> has no image.
thanks,
I see. Try this:
===============================================
require 'rubygems'
require 'hpricot'

doc = Hpricot %q{<p class=posted>
this is fun
<img src="" class="dhans"/>
</p>
<p class=posted>
NO FUN
</p>
<p class=posted>
fun again!
<img src=""/>
</p>
<p class=posted>
NO FUN AT ALL!
</p>
}

paragraphs = doc/'p'

good_elems = paragraphs.map.reject {|elem| ((elem/"img").empty?) }
good_elems.each { |elem| puts elem.inner_text.strip }
===============================================

output:

************
this is fun
fun again!
************


You will need hpricot 0.4.84 because of inner_text - if you don't want
to install it (I did not experience any difficulties, so I can recommend
it) then you have to roll your own inner_text, but I guess this is not a
big problem.

Cheers,
Peter
 
D

David Vallner

--------------enig2794C5B87AD68CF9346CBEC9
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Peter said:
paragraphs =3D doc/'p'
=20
good_elems =3D paragraphs.map.reject {|elem| ((elem/"img").empty?) }

Which once again makes me wish paragraphs =3D doc/'//p/text()'
worked. This could be doable if you asked Hpricot to provide you with
the REXML document (it's probably out of scope for the intendedly simple
XPath engine Hpricot uses natively), but unfortunately I can't for the
heck of it figure out how to make REXML accept the final /text(), even
though the parser claims to support XPath 1.0 except a few exceptions,
that one not being noted.

David Vallner


--------------enig2794C5B87AD68CF9346CBEC9
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFgIyEy6MhrS8astoRAn1VAJ0cxjuJeKzj0kPPb0Oa6zXHlR2cuACfUVAo
m1dOyjfNUiBee45Brzgz9Gc=
=GjVT
-----END PGP SIGNATURE-----

--------------enig2794C5B87AD68CF9346CBEC9--
 
R

ruby talk

Ask:

http://code.whytheluckystiff.net/hpricot/ticket/32

text in xpath should return a text node if present. For example:
(doc/"/html/body/div[1]/*/table[0]/tr[0]/*/b[9]/text")

Currently I am using the search and next_node:

doc.search("/html/body/div[1]/*/table[0]/tr[0]/td/b"){|x|
@movie_plot=x.next_node.to_s.strip if x.inner_html=="Plot Outline:" }


And receive

Author:
why
Message:

* lib/hpricot/elements.rb: added support for selecting text
nodes with text(): //p/text(), //p[a]//text(), etc.
* lib/hpricot/traverse.rb: ditto.
* lib/hpricot/tag.rb: the pathname method reports the path
fragment needed to get to this node.
* lib/hpricot/parse.rb: handle possible empty processing instruction.
http://code.whytheluckystiff.net/hpricot/changeset/87




Peter said:
paragraphs = doc/'p'

good_elems = paragraphs.map.reject {|elem| ((elem/"img").empty?) }

Which once again makes me wish paragraphs = doc/'//p/text()'
worked. This could be doable if you asked Hpricot to provide you with
the REXML document (it's probably out of scope for the intendedly simple
XPath engine Hpricot uses natively), but unfortunately I can't for the
heck of it figure out how to make REXML accept the final /text(), even
though the parser claims to support XPath 1.0 except a few exceptions,
that one not being noted.

David Vallner
[/QUOTE]
 
D

David Vallner

--------------enig33E9091034D4C2BDEBAF685B
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

ruby said:
Ask:
=20
http://code.whytheluckystiff.net/hpricot/ticket/32
=20
text in xpath should return a text node if present. For example:
(doc/"/html/body/div[1]/*/table[0]/tr[0]/*/b[9]/text")
=20

Well, it's 'text()' not 'text'. Luckily _why noticed.
* lib/hpricot/elements.rb: added support for selecting text
nodes with text(): //p/text(), //p[a]//text(), etc.

W00t ;P

Thanks for pointing this out.

David Vallner

PS: Your email address name confuses the heck out of me. Please use
something that doesn't cause a mental namespace clash?


--------------enig33E9091034D4C2BDEBAF685B
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFhCpcy6MhrS8astoRAqATAJ9aaFisLlRV2vMdrMpycIj+uWgjZACdE5iQ
F4GF/Ef4SAkCrZ5Gss/B1mo=
=jQhx
-----END PGP SIGNATURE-----

--------------enig33E9091034D4C2BDEBAF685B--
 
R

ruby talk

ruby said:
Ask:

http://code.whytheluckystiff.net/hpricot/ticket/32

text in xpath should return a text node if present. For example:
(doc/"/html/body/div[1]/*/table[0]/tr[0]/*/b[9]/text")

Well, it's 'text()' not 'text'. Luckily _why noticed.
* lib/hpricot/elements.rb: added support for selecting text
nodes with text(): //p/text(), //p[a]//text(), etc.

W00t ;P

Thanks for pointing this out.

David Vallner

PS: Your email address name confuses the heck out of me. Please use
something that doesn't cause a mental namespace clash?


Sorry, I have been archiving ruby talk at (e-mail address removed) since 10/14/04.

Stephen Becker IV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top