这是指向实际 HTML 的链接:
doc = Nokogiri::HTML(open('https://www.google.com/finance?q=NYSE%3AAA&fstype=iii'))
以及一个近似的 HTML 片段:
<div id="incannualdiv">
<table id="fs-table">
<tbody>
<tr>..</tr>
...
<tr>
<td>Net Income</td>
<td>100</td>
</tr>
<tr>..</tr>
</tbody>
</table>
</div>
这些是我正在使用的细节:
doc = Nokogiri::HTML(open('https://www.google.com/finance?q=NYSE%3AAA&fstype=iii'))
div = doc.at "div[@id='incannualdiv']" #div containing the table i want
table = div.at 'table' #table containing tbody i want
tbody = table.at 'tbody' #tbody containing tr's I want
trs = tbody.at 'tr' #SHOULD be all tr's of that table/tbody - but it's only the first TR?
我希望最后一点能给我所有的<TR>
标签,其中包括<TD>
我正在寻找的标签,但实际上它只给了我第一个<TR>
.
这是下一块:
irb(main):023:0> a = nil
=> nil
irb(main):024:0> doc.css('#incannualdiv > #fs-table tr').each { |e| if e.text.include? "Net Income\n"; a = e.text; end}
=> 0
irb(main):025:0> a
=> "Net Income\n\n191.00\n611.00\n254.00\n-1,151.00\n"
irb(main):026:0> a.split"\n"
=> ["Net Income", "", "191.00", "611.00", "254.00", "-1,151.00"]