如何在属性中显示所有已删除的数据?

时间:2012-12-18 10:30:53

标签: screen-scraping jruby web-scraping htmlunit jrubyonrails

我正在尝试使用Jruby和HtmlUnit从此页面http://www.momondo.com/multicity/?Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=20-01-2013&AD=5&DO=false&NA=false#Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=20-01-2013&AD=5&DO=false&NA=false解析总价格。当我使用puts price

时,结果在控制台中很好地显示

€96 Total €96 Total €96 Total €109 Total €109 Total €128 Total €128 Total €172 Total €197 Total €197 Total €228 Total €270 Total €270 Total €270 Total €270 Total

现在我正在尝试将所有值更新到db表的属性名为price并在视图中显示。但视图仅显示视图中所有值的最后一个值€270 Total。有什么问题?

我的代码:

desc "Fetch details"
task :fetch_details => :environment do

require 'java';

require 'commons-codec-1.7.jar';
require 'commons-collections-3.2.1.jar';
require 'commons-io-2.4.jar';
require 'commons-lang3-3.1.jar';
require 'commons-logging-1.1.1.jar';
require 'cssparser-0.9.8.jar'
require 'htmlunit-2.11.jar'
require 'htmlunit-core-js-2.11.jar'
require 'httpclient-4.2.2.jar'
require 'httpcore-4.2.2.jar'
require 'httpmime-4.2.2.jar'
require 'jetty-http-8.1.7.v20120910.jar'
require 'jetty-io-8.1.7.v20120910.jar'
require 'jetty-util-8.1.7.v20120910.jar'
require 'jetty-websocket-8.1.7.v20120910.jar'
require 'nekohtml-1.9.17.jar'
require 'sac-1.3.jar'
require 'serializer-2.7.1.jar'
require 'xalan-2.7.1.jar'
require 'xercesImpl-2.10.0.jar'
require 'xml-apis-1.4.01.jar'
require 'open-uri'
require "erb"

include ERB::Util

java_import 'com.gargoylesoftware.htmlunit.WebClient';

wc = WebClient.new;
Product.find_all_by_price(nil).each do |update|
  page = wc.getPage("http://www.momondo.com/multicity/?Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=20-01-2013&AD=5&DO=false&NA=false#Search=true&TripType=oneway&SegNo=1&SO0=KUL&SD0=KBR&SDP0=20-01-2013&AD=5&DO=false&NA=false");
  sleep(30)
  page.getByXPath('//*[contains(concat( " ", @class, " " ), concat( " ", "total", " " ))]').each do |a|
    price = a.asText
    puts price
    update.update_attribute(:price, price)
  end
end
end

如果我需要澄清任何事情,请告诉我。非常感谢!

0 个答案:

没有答案