0

I'm trying to get table with content of MMEL codes from this site and I'm trying to accomplish it with CSS Selectors.

私がこれまでに持っているものは次のとおりです。

require_relative 'sources/Downloader'
require 'nokogiri'

html_content = Downloader.download_page('http://www.s-techent.com/ATA100.htm')
parsed_html = Nokogiri::HTML(html_content)

tmp = parsed_html.css("tr[*]")

puts tmp.text

そして、属性でこれを取得しようとするとエラーが発生trします。JSON に解析したいので、このテーブルを単純な形式で取得するには、このタスクをどのように完了することができますか。これをセクションで取得して、.eachブロックで呼び出すとよいでしょう。


編集: このようなブロックで物事を取得できれば、私はニックになります(ページのソースを調べてください)

<TR><TD WIDTH="10%" VALIGN="TOP" ROWSPAN=5>
<B><FONT FACE="Arial" SIZE=2><P ALIGN="CENTER">11</B></FONT></TD>
<TD WIDTH="40%" VALIGN="TOP"  COLSPAN=2>
<B><FONT FACE="Arial" SIZE=2><P>PLACARDS AND MARKINGS</B></FONT></TD>
<TD WIDTH="50%" VALIGN="TOP">
<FONT FACE="Arial" SIZE=2><P ALIGN="LEFT">All procurable placards, labels, etc., shall be included in the illustrated Parts Catalog.  They shall be illustrated, showing the part number, Legend and Location.  The Maintenance Manual shall provide the approximate Location (i.e., FWD -UPPER -RH) and illustrate each placard, label, marking, self -illuminating sign, etc., required for safety information, maintenance significant information or by government regulations.  Those required by government regulations shall be so identified.</FONT></TD>
</TR>
4

2 に答える 2

1

これにより、ソースからのすべての TR が 96 行目に出力されます。そのページには 3 つの表があり、table[1]必要なすべてのテキストが含まれています。

require 'nokogiri'

doc = Nokogiri::HTML(open('http://www.s-techent.com/ATA100.htm'))
doc.css("table")[1].css("tr").each do |i|
  puts i #=> prints the exact html between TR tags (including)
  puts i.text #=> prints the text
end

例えば:

puts doc.css("table")[1].css("tr")[2] 

以下を出力します。

<tr>
<td valign="TOP" colspan="3">
<b><font face="Arial" size="2"><p align="CENTER">GROUP DEFINITION - AIRCRAFT</p></font></b>
</td>
<td valign="TOP">
<font face="Arial" size="2"><p align="LEFT">The complete operational unit.  Includes dimensions and
areas, lifting and shoring,    leveling and weighing, towing and taxiing, parking and mooring, requi
red placards, servicing.</p></font>
</td>
</tr>
于 2013-09-07T21:27:47.273 に答える
1

xpath以下を使用しても同じことができます。

以下は、OP による投稿で指定された Web ページの最初の表の内容です。

require 'nokogiri'
require 'open-uri'

doc = Nokogiri.HTML(open('http://www.s-techent.com/ATA100.htm'))
doc.xpath('(//table)[1]/tr').each do |tr|
  puts tr.to_html(:encoding => 'utf-8')
end

出力:

  <tr>
  <td width="33%" valign="MIDDLE" colspan="2">
  <p><img src="S-Tech-Logo-Blue2.gif" width="274" height="127"></p>
  </td>
  <td width="67%" valign="MIDDLE">
  <b><i><font face="Arial" color="#0000ff">
  <p align="CENTER"><big>AIRCRAFT PARTS MANUFACTURING ASSISTANCE (PMA)</big><br><big>DAR SERVICES</big></p></font></i></b>
  </td>
  </tr>

ここで、最後のテーブル行を収集する場合は、次のようにします。

require 'nokogiri'
require 'open-uri'

doc = Nokogiri.HTML(open('http://www.s-techent.com/ATA100.htm'))
p doc.xpath('(//table)[3]/tr').to_a.size # => 1
doc.xpath('(//table)[3]/tr').each do |tr|
  puts tr.to_html(:encoding => 'utf-8')
end

出力:

<tr>
<td width="40%" valign="TOP" height="10">
<p align="CENTER"><b><font face="Arial" size="2" color="#0000ff">149 AZALEA CIRCLE • LIMERICK, PA 19468-1330</font></b></p>
</td>
<td width="30%" valign="TOP" height="10">
<p align="CENTER"><b><font face="Arial" size="2" color="#0000ff">610-495-6898 (Office) • 484-680-0507 (Cell)</font></b></p>
</td>
<td width="110%" valign="TOP" height="10">
<p align="CENTER"><a href="Contact.htm"><b><font face="Arial" size="2">E-mail S-Tech</font></b></a></p>
</td>
</tr>
于 2013-09-07T22:10:04.283 に答える