0

I've got this array coming back from a web scrape. Which looks like this:

[["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"]]

So what happens is that it duplicates? So i'm wanting to change the above array to this (it will be different everytime so it'd need to remove duplicates.):

[["formatted_sum_fees", "£5.60"],
["formatted_price", "£46.50"],
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"],
["formatted_sum_fees", "£3.30"],
["formatted_price", "£27.50"]

Anything else that exists after this is a dupe.

What i'm needing is the fees and the price to be on a var so i can save it down to the database :)

Thanks Sam

extra Heres the raketask.

require "nokogiri"
require "open-uri"
namespace :task do
  task test: :environment do
    ticketmaster_url = "http://www.ticketmaster.co.uk/derren-brown-miracle-glasgow-04-07-2016/event/370050789149169E?artistid=1408737&majorcatid=10002&minorcatid=53&tpab=-1"
     doc = Nokogiri::HTML(open(ticketmaster_url))
     event_name = nil
     ticket_price = nil
     doc.xpath("//script[@type='text/javascript']/text()").each do |text|
       if text.content =~ /more_options_on_polling/
         ticket_price = text.to_s.scan(/\"(formatted_(?:price|sum_fees))\":\"(.+?)\"/)
         byebug
       end
     end

  end
end

2 Answers 2

1

you just need to add uniq method on array that you've got from web scrape, which will give you an uniq values from that array, and then you can easily iterate on that array to store values into database.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use like

[["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£5.60"], 
["formatted_price", "£46.50"], 
["formatted_sum_fees", "£4.50"], 
["formatted_price", "£37.50"], 
["formatted_sum_fees", "£3.30"], 
["formatted_price", "£27.50"]].uniq

Then result is:

 [["formatted_sum_fees", "£5.60"], ["formatted_price", "£46.50"], ["formatted_sum_fees", "£4.50"], ["formatted_price", "£37.50"], ["formatted_sum_fees", "£3.30"], ["formatted_price", "£27.50"]]

1 Comment

I'm sorry i don't understand this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.