1

我正在尝试从此页面解析总价格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使用 Jruby 和 HtmlUnit。当我使用时,结果很好地显示在控制台中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

现在我正在尝试更新我进入名为 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

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

4

0 回答 0