我很难从航班结果中提取总价(css 选择器 = '.total')。
http://www.momondo.com/multicity/?Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=31-12-2012&AD=2&CA=0,0&DO=false&NA=false#Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=31-12-2012&AD=2&CA=0,0&DO=false&NA=false
我收到错误“nil:NilClass nokogiri 的未定义方法‘文本’”。
我的代码
desc "Fetch product prices"
task :fetch_details => :environment do
require 'nokogiri'
require 'open-uri'
include ERB::Util
OneWayFlight.find_all_by_money(nil).each do |flight|
url = "http://www.momondo.com/multicity/Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=31-12-2012&AD=2&CA=0,0&DO=false&NA=false#Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=31-12-2012&AD=2&CA=0,0&DO=false&NA=false"
doc = Nokogiri::HTML(open(url))
price = doc.at_css(".total").text[/[0-9\.]+/]
flight.update_attribute(:price, price)
end
end