用于解析的Selenium Web驱动程序(Ruby)
Selenium-webdriver for parsing (Ruby)
因此,这是一个使用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选择器。
如果我应该添加其他信息,请告诉我
页面的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>
第一个findElements返回20个项目,您的代码块将单击每个项目。
但是,你的第二个findElements返回了48个项目,并且从你的代码块中返回,我不明白你的代码模块试图实现什么。
'.p3a,.ffirm,.p2表tr:nth child(1).p,.p2表格tr:nh child(2).p,p2表tr:nth child(3).p,.p2表tr:nh child(4).p'css选择器返回所有匹配的元素(","用作"或"分隔符)。
但是,迭代数组一次只返回一个元素。你是否认为你可以在每次迭代中访问一家公司的所有字段?如果是这样,你就不能。
使用它将返回20个地址块;
driver.find_elements
(:css, 'div[id*='adressSelector']')
YOu可以迭代这些,执行findElement来获得所需的字段。
- Javascript:selenium Web驱动程序isDisplayed()不工作
- Selenium Web驱动程序和javascript
- PhantomJS重影驱动程序停止
- Selenium驱动程序不反映单击()后的页面更改
- Xpath对Selenium Web驱动程序无效”;Xpath未找到”;
- 等待,直到使用Java在selenium网络驱动程序中手动单击登录按钮
- Sencha禁用按钮和Selenium Web驱动程序
- 通过web驱动程序异步执行Javascript
- 如何使用Python/Selenium网络驱动程序处理Angularjs/Javascript下拉列表
- 如何使用Selenium Web驱动程序验证电子邮件
- 如何在另一个 html 标签内选择 web 元素硒 Web 驱动程序与 html 源
- 网络驱动程序中使用的向下滚动功能从网页的哪个部分进行搜索
- Minitest/Capibara:切换到JS驱动程序时,绿色测试变为红色
- 如何用Javascript编写设备驱动程序
- 未从Selenium网络驱动程序中的配置文件接收到正确的值
- Mongodb节点驱动程序2.0.*与Bluebird 2.9.*承诺
- Node.js Mongodb原生驱动程序连接共享
- RxJS:在循环中处理错误.js自定义驱动程序
- 是否可以在页面开始加载时使用 Ruby 的硒网络驱动程序execute_script
- 用于解析的Selenium Web驱动程序(Ruby)