Basically all I would like to do is export a whole html table to a .txt file (notepad document).
So far I have learnt how to instruct the browser to find the html page with the table.
require 'rubygems'
require 'hpricot'
require "watir-webdriver"
url = "http://www.example.com"
browser = Watir::Browser.new
browser.goto url
After running the above in cmd I can now see the html table in the browser.
This is where I am stuck. How do I use Watir to
- Find the tag
- collect everything (i.e. the html , and the text) which is within and .
- Extract those results to a .txt file (notepad document) and save it in a specific folder.
FYI the html table looks like this…
<table border="1" cellpadding="2">
<tr>
<th> Address </th>
<th> Council tax band </th>
<th> Annual council tax </th>
</tr>
<tr>
<td> 2, STONELEIGH AVENUE, COVENTRY, CV5 6BZ </td>
<td align="center"> F </td>
<td align="center"> £2125 </td>
</tr>
……. The above row is repeated many time ……
</table>
Then the table is closed.
So to re-cap my situation. I can use Watir to navigate the browser to the page containing the html table but my problem is that I am unsure of how to extract the results (everything within the tag – including the html) to a .txt file and then save that .txt file onto my computer.
I would prefer to take smaller steps with using Watir. I am knew to it therefore I would just like to learn how to extract the table and save everything that I have extracted into a .txt file. I have seen a couple of examples online using hpricot. However most of the examples seem to miss off code detailing how the array (if that is the correct approach) is outputted into a .txt file.
Could you help by demonstrating how to write a simple piece of code which will extract the html table ( and everything, including the , and everything in between) to a .txt notepad file?
Many thanks for your time.
To get HTML of the entire table (if it is the only table on the page):
You will get something like this:
To get HTML of each row and put it in an array:
To get text of each cell and put it in an array:
To write text of each cell to file:
The file will look like this: