I am new to Ruby can someone help me please?
below is my html structure, I want to get ‘X01FJ65K0M’ from td. There is
no id on this table.
Thanks in advance
Request Type | Agent Type | |
---|---|---|
X01FJ65K0M |
07/03/2014 08:14:42 |
I am new to Ruby can someone help me please?
below is my html structure, I want to get ‘X01FJ65K0M’ from td. There is
no id on this table.
Thanks in advance
Request Type | Agent Type | |
---|---|---|
X01FJ65K0M |
07/03/2014 08:14:42 |
First a question; do you want to retrieve data from HTML on a regular
basis or often in the same program? In this case use the Nokogiri-gem :
------------------ untested ------------
require ‘nokogiri’
If you need this just once, read the file line by line to detect the
table-row of the right CSS-class, than find the very first td in this
row.
See File.readlines. The procedure may depend on the actual formating of
the HTML-code. If the code is not “beautified”, you will need something
like String.match in addition.
But I would use Nokogiri anyway.
Michael U. wrote in post #1158939:
First a question; do you want to retrieve data from HTML on a regular
basis or often in the same program? In this case use the Nokogiri-gem :------------------ untested ------------
require ‘nokogiri’File.open(‘htmlfile’, “r”) do |infile|
htmldoc = Nokogiri::HTML::Document.parse(infile)
data = htmldoc.xpath("//td").first.content
end
Thanks for this. It helped me partially. I am not using any file to
parse. Its a table in web page. I want to get td from tr.How do I
iterate ?
this is my xpath: table/tr[2]/td[4].
det_page is web page opened by nokogiri.
If I want to have this xpath in a loop how do I do ? Below does not
execute.
p = 2
apptpath = det_page.xpath("//table/tr[p]/td[4]").each do |row|
row.xpath(’./tr[p]’).each do |tr|
row.xpath(’./td[4]’).each do |td|
puts td.text
p = p+1
end
end
end
Aparna Kya wrote in post #1159965:
If I want to have this xpath in a loop how do I do ? Below does not
execute.
Line 2: You iterate all the cells in the fourth column of the third and
all the later rows of a table. You do not iterate rows.
Line 4: You iterate over all the rows in third and later positions. How
many tables do you expect in the HTML-FILE that you are handling?
Line 6: does not make much sense, as you iterate over each fourth cell
in the cells that you are iterating over in the outer loop.
Line 10: You can probably skip the variable p, once that your calls to
each are managed.
To achieve a better understanding of what is going on, try not to
finalize your program with mysterious code. Begin with some
experimentation. For example, try to puts() the kind of tags, that your
xpath produces.
puts det_page.xpath("//table/tr[p]/td[4]").to_s
puts det_page.xpath("//table/tr[p]/td[4]").size
puts det_page.xpath("//table/tr[p]/td[4]")[0].name
puts det_page.xpath("//table/tr[p]/td[4]")[0].node_type
and so on… see :$> ri Nokogiri::XML::Node for things to try.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs