# Using Hpricot with HTML's

**URL:** <https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909>\
**Category:** Ruby\
**Created:** [November 22, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909 "2008-11-22T20:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ukbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@ukbell](https://www.ruby-forum.com/u/ukbell)\
**Post date:** [November 22, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909/1 "2008-11-22T20:47:32Z")

</div>

I have been working on this program for class all day and am not getting  
anywhere, so I figured I would try to ask you guys on the forum. Here is  
my code so far:

#!/usr/bin/ruby

# Lab 19

require ‘rubygems’  
require ‘fileutils’  
require ‘hpricot’  
require ‘net/http’

tamper\_args = {  
‘my\_countries’ =\> ‘,“Germany”, “Japan”, “United Kingdom”, “United  
States”’,  
‘my\_vars’ =\> ‘,+pop’,  
‘my\_yrs’ =\> ‘,1950,1960,1970,1980,1990,2000’,  
‘selected\_vars’ =\> ‘POP’,  
‘format\_selected’ =\> ‘csv’  
}

host\_name =  
“[http://pwt.econ.upenn.edu/php\_site/pwt62/pwt62\_retrieve.php](http://pwt.econ.upenn.edu/php_site/pwt62/pwt62_retrieve.php)”

puts tamper\_screen = Net::HTTP.post\_form(URI.parse(host\_name),  
tamper\_args)  
puts page\_parse = Hpricot(tamper\_screen.to\_s)

puts page\_parse.search(“pre”)

* * *

It is designed to go through the host\_name link and give the populations  
for the 4 countries listed, with years 1950, 1960… through 2000, and  
I have to get the data in csv format. I am not sure why I am not able to  
do my search the pre tag, which is where the data is stored in the html  
doc.

I think it has to do with the .to\_s for the page\_parse variable, but my  
program gives me an error when tamper\_screen is not a string.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![ukbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@ukbell](https://www.ruby-forum.com/u/ukbell)\
**Post date:** [November 22, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909/2 "2008-11-22T22:13:27Z")

</div>

Hi Rick,

On Sun, Nov 23, 2008 at 04:43:20AM +0900, Rick Rick wrote:

> require ‘hpricot’
> 
> * * *
> 
> It is designed to go through the host\_name link and give the populations  
> for the 4 countries listed, with years 1950, 1960… through 2000, and  
> I have to get the data in csv format. I am not sure why I am not able to  
> do my search the pre tag, which is where the data is stored in the html  
> doc.
> 
> I think it has to do with the .to\_s for the page\_parse variable, but my  
> program gives me an error when tamper\_screen is not a string.

Try using the body method. post\_form returns a response object, and you  
need to grab the body of the response:

require ‘rubygems’  
require ‘fileutils’  
require ‘hpricot’  
require ‘net/http’

tamper\_args = {  
‘my\_countries’ =\> ‘,“Germany”, “Japan”, “United Kingdom”, “United  
States”’,  
‘my\_vars’ =\> ‘,+pop’,  
‘my\_yrs’ =\> ‘,1950,1960,1970,1980,1990,2000’,  
‘selected\_vars’ =\> ‘POP’,  
‘format\_selected’ =\> ‘csv’  
}

host\_name =  
“[http://pwt.econ.upenn.edu/php\_site/pwt62/pwt62\_retrieve.php](http://pwt.econ.upenn.edu/php_site/pwt62/pwt62_retrieve.php)”

tamper\_screen = Net::HTTP.post\_form(URI.parse(host\_name), tamper\_args)

page\_parse = Hpricot(tamper\_screen.body)  
puts page\_parse.search(“pre”).inner\_text

---

<div class="post-metadata">

**Author:** ![ukbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@ukbell](https://www.ruby-forum.com/u/ukbell)\
**Post date:** [November 22, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909/3 "2008-11-22T23:12:46Z")

</div>

Thank you so much!!! I can finally get past that part.

---

<div class="post-metadata">

**Author:** ![ukbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/898d66/32.png) [@ukbell](https://www.ruby-forum.com/u/ukbell)\
**Post date:** [November 23, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/using-hpricot-with-htmls/153909/4 "2008-11-23T14:57:50Z")

</div>

Just to continue along with this same program, I am now trying to change  
my parameters for the tamper\_args to allow the user to key in a country  
name, and the program automatically changes the country in  
‘my\_countries’ to whatever counties the user wants to examine… Is  
there a way to allow for this?
