jeff m
jeff m

Reputation: 275

Extracting text from a webtable in Watir / Ruby

I'm trying to extract the data from an Income Statement, url is http://finance.yahoo.com/q/is?s=LMT+Income+Statement&annual

I was unable to find the table using the browser.table(:name, 'blah') or (:id, 'blah'), but had some luck using the xpath with Nokogiri using this code, which picks up after I've initialized everything and browsed to the page:

page_html = Nokogiri::HTML.parse(browser.html)
tobj = page_html.xpath('//*[@id="yfncsumtab"]').inner_text

Now I'm able to take tobj and pull the data out, but it doesn't do me any good for trying to manipulate the object as a table. Any suggestions on how to go about storing the table as a variable would help. I can probably figure out iterating through the rows/columns from there, but I wouldn't mind if you tacked on some code that would do that.

Upvotes: 1

Views: 835

Answers (3)

Si Cheng Zhou
Si Cheng Zhou

Reputation: 36

Try browser.element(id: "yfncsumtab").text

Upvotes: 0

Željko Filipin
Željko Filipin

Reputation: 57312

Do you know Watir has xpath support?

browser.element(:xpath => '//*[@id="yfncsumtab"]')

Upvotes: 1

pguardiario
pguardiario

Reputation: 55002

Look at it this way:

doc = Nokogiri::HTML.parse(browser.html)
table = doc.at('table#yfncsumtab')

# iterate through tr's
table.search('tr').each do |tr|
  # do something with tr
end

Upvotes: 0

Related Questions