ページの html はあまり良くありません。つまり、データを関連付けるための適切な識別子がありません。たとえば、会社名を住所に関連付けることができるのは相対位置だけです。
以下の解決策は、脆弱なテキストの配置を前提としていますが、私が考えることができる最高のものです.
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.navigate.to 'http://www.ypag.ru/cat/komp249/page0.html'
# The table that contains all of the data
# This xpath is not ideal (brittle) but I could not find a better identifier)
table = driver.find_element(:xpath, '/html/body/table[4]/tbody/tr/td[2]/table')
# Expand all of the address links
table.find_elements(:css, 'a[href *= loadadress]').each(&:click)
# Get all of the rows that contain data
# Need to ignore blanks, ads, etc.
data_elements = table.find_elements(:xpath, './tbody/tr').keep_if do |row|
row.find_elements(:css, '.p3, .p, .p2').length > 0
end
# Of the rows we have, each set of three rows represents a company
# Iterate through each set of three rows to collect data
data_elements.each_slice(3) do |company|
name = company[0].find_element(:css, '.p3').text
firm = company[0].find_element(:css, '.firm').text
firm_split = firm.split(' » ')
country = firm_split[0]
city = firm_split[1]
description = company[1].text
# Get the address values matching, using the icons to determine the rows meaning
# Note that not every company has each detail, in which case the value will be ''
url = ''
email = ''
phone = ''
address = ''
# Wait to ensure the address block has been loaded
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
element = wait.until { company[2].find_element(:css, 'div[id*=adressSelector]') }
end
sub_table_data = company[2].find_elements(:css, 'div[id*=adressSelector] tr')
sub_table_data.each do |row|
cells = row.find_elements(:css, 'td')
case cells[0].find_element(:css, 'img').attribute('src')
when /papers/
url = cells[1].text
when /mail/
email = cells[1].text
when /mobile/
phone = cells[1].text
when /map/
address = cells[1].text
end
end
# Output the results (or whatever you want them for)
puts name
puts country
puts city
puts description
puts url
puts email
puts phone
puts address
puts
end
例として、上記のコードは最初の会社に関する次の詳細を提供します (これは英語に翻訳されたページからのものであることに注意してください)。
Storm-Print
Russia »Moscow
Printing Services: stationery, flyers, leaflets, brochures.
http://www.storm-print.ru
info@storm-print.ru
+7 (495) 101-37-62 multichannel Fax: +7 (495) 101-37-62 multichannel
Russia "Moscow ul.Suschevsky shaft 16, page 4, 127018
参考までに、会社の html は次のようになります。
<tr>
<td align="left" class="p3">
<a href="http://www.msyp.ru/cat/kompaniy992511/s-779665944.html">
<font>
<font class="">
Storm-Print
</font>
</font>
</a>
</td>
<td align="right" class="firm">
<font>
<font>
Russia >
Moscow
</font>
</font>
</td>
</tr>
<tr>
<td align="left" colspan="2" width="100%" class="p">
<font>
<font class="">
Printing Services: stationery, flyers, leaflets, brochures.
</font>
</font>
<br>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<font>
<font>
Rating:
</font>
</font>
<a class="iframe2" href="reit/r.php?id=992511">
<img src="fon/star_reit_off.png" border="0">
<img src="fon/star_reit_off.png" border="0">
<img src="fon/star_reit_off.png" border="0">
<img src="fon/star_reit_off.png" border="0">
<img src="fon/star_reit_off.png" border="0">
</a>
</td>
</tr>
<tr>
<td colspan="2">
<table class="p2" border="0" width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td align="left">
<div id="adressSelector992511">
<table>
<tbody>
<tr>
<td>
<img src="http://www.ypag.ru/fon/papers.gif" border="0">
</td>
<td class="p">
<a href="http://www.storm-print.ru" target="_blank">
<font>
<font class="">
http://www.storm-print.ru
</font>
</font>
</a>
</td>
</tr>
<tr>
<td>
<img src="http://www.ypag.ru/fon/mail.gif" border="0">
</td>
<td class="p">
<a href="mailto:info@storm-print.ru">
<font>
<font class="">
info@storm-print.ru
</font>
</font>
</a>
</td>
</tr>
<tr>
<td>
<img src="http://www.ypag.ru/fon/mobile.gif" border="0">
</td>
<td class="p">
<font>
<font class="">
+7 (495) 101-37-62 multichannel Fax: +7 (495) 101-37-62 multichannel
</font>
</font>
</td>
</tr>
<tr>
<td>
<img src="http://www.ypag.ru/fon/map.gif" border="0">
</td>
<td class="p">
<font>
<font class="">
Russia "Moscow ul.Suschevsky shaft 16, page 4, 127018
</font>
</font>
</td>
</tr>
<tr>
<td>
<img src="http://ypag.ru/fon/editdelete.png" border="0">
</td>
<td align="left" class="p">
<a href="http://www.ypag.ru/edit_kompany.php?idkomp=992511&c=3770450052" target="_blank" onclick="popupWin = window.open(this.href, 'contacts', 'location,width=600,height=500,top=0,scrollbars=yes'); popupWin.focus(); return false;">
<font>
<font>
Report incorrect data
</font>
</font>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>