【发布时间】:2015-01-09 03:00:40
【问题描述】:
我正在尝试解析以下website 以获取弹出框中包含的纬度和经度,但似乎无法正常工作。我在 Ruby 中使用 Watir 和 Nokogiri。代码如下:
require 'watir'
require 'nokogiri'
require 'win32ole'
require 'open-uri'
# Get filename from user
puts "What is the name of the excel file?"
file_name1 = gets.chomp
file_name2 = file_name1 << '.xlsx'
# WIN32OLE
excel = WIN32OLE::new('excel.Application')
excel.visible = true
filepath = excel.Workbooks.Open('C:/users/desktop/ruby/' << file_name2)
url = 'http://webapps2.rrc.state.tx.us/EWA/drillingPermitsQueryAction.do'
# Excel Column Headers
excel.worksheets(2).Cells(1,12).value = "Latitude"
excel.worksheets(2).Cells(1,13).value = "Longitude"
# Watir
browser = Watir::Browser.new # opens new IE browser
browser.speed = :zippy
browser.goto url # goes to RRC page
row = 2
while excel.worksheets(2).Cells(row,5).value.nil? == false
browser.text_field(:name, 'searchArgs.apiNoHndlr.inputValue').set excel.worksheets(2).Cells(row,5).value.to_s[0..7]
browser.button(:value, 'Submit').click # Clicks the submit button
browser.select_list(:name, "propertyValue").select 'GIS Viewer'
page_html = Nokogiri::HTML.parse(browser.html)
latitude = page_html.css("#printIdentifyWellDiv > table:nth-child(5) > tbody > tr:nth-child(7) > td").text.strip
longitude = page_html.css("#printIdentifyWellDiv > table:nth-child(5) > tbody > tr:nth-child(8) > td").text.strip
excel.worksheets(2).Cells(row,12).value = latitude
excel.worksheets(2).Cells(row,13).value = longitude
browser.window(:title => "RRC Public GIS Viewer").use do
browser.button(:id => "close").click
end
browser.button(:value, 'Return').click
row += 1
end
puts "Complete"
问题出在第 34 和 35 行(纬度和经度变量)。 Nokogiri 似乎无法从弹出窗口中解析它们并将它们移动到 Excel 文件中。我尝试使用 Xpath 和 CSS 路径,但没有任何成功。每次我运行程序时,相应的 Excel 文件都会在纬度和经度列中显示为空白。
问题:
- 如何解析数据?
- 当我的程序运行时,上面链接的地图屏幕显示在浏览器的第二个选项卡中。
这是 Watir/Nokogiri 的问题吗?我是否需要在程序中以某种方式选择该选项卡以便 Nokogiri 能够解析它?
感谢您的宝贵时间。
【问题讨论】: