0
我試圖解析下面的website包含在彈出框中的緯度和經度,似乎無法使其工作。我在Ruby中使用Watir和Nokogiri。代碼如下:在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能夠解析它嗎?
謝謝你的時間。
謝謝你提多書,這解決了我的問題之前。我感謝你的時間 – Troy