1

所以,这里是使用javascript显示公司信息的网站(首先翻译成英文)(您需要点击“地址和电话号码”)。我是用 selenium 完成的(点击链接),现在我正在尝试使用 css 选择器收集有关这些公司(电话、地址等)的信息并保存到数据库中。但是我无法正确地将信息保存到数据库中,因为我无法将有关公司的信息保存在所需的变量中。

这是我的代码(这是错误的):

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get 'http://www.ypag.ru/cat/komp249/page0.html'


driver.find_elements(:css, '.p2 div a').each {|link| link.click}
driver.find_elements
(:css, '.p3 a, .firm, .p2 table tr:nth-child(1) .p, .p2 table tr:nth-child(2) .p,
p2 table tr:nth-child(3) .p, .p2 table tr:nth-child(4) .p').each {|n,r,c,k,l,m| 
name = n
region = r
field1 = c
field1 k
field1 l
field1 m }

我的目的是将每个 css 选择器保存在正确的变量中,这可能吗?我已经问过这个问题,但是我没有用于地址、电话等的 CSS 选择器。

如果我应该添加其他信息,请告诉我

4

2 回答 2

0

页面的 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&amp;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>
于 2013-08-27T17:14:00.250 回答
0

第一个 findElements 返回 20 个项目,您的代码块将单击每个项目。

但是,您的第二个 findElements 从您的代码块返回 48 个项目,我不明白您的代码块试图实现什么。

'.p3 a, .firm, .p2 表 tr:nth-child(1) .p, .p2 表 tr:nth-child(2) .p, p2 表 tr:nth-child(3) .p, .p2 table tr:nth-child(4) .p' css 选择器返回所有匹配的元素(“,”用作“或”分隔符)。

但是,迭代数组一次只返回一个元素。您是否认为您可以在每次迭代中访问一家公司的所有字段?如果是这样,你不能。

使用它将返回 20 个地址块;

driver.find_elements
(:css, 'div[id*='adressSelector']')

你可以迭代这些,执行 findElement 来获取你想要的字段。

于 2013-08-27T16:07:28.657 回答