-2
require "openssl"
require "nokogiri"
require 'csv'
require "open-uri"
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
$n=0

#~ Open_Page
page = ('http://www.residentadvisor.net/dj/aguycalledgerald/tracks?sort=mostcharted')
html = Nokogiri::HTML(open(page))

#~ Array
names= []
html.css('a').each do |x|
  names<< x.text.strip.gsub(/\t/,'')
  names.delete('RA on YouTube')
  names.delete('Login')
  names.delete('Register')
  names.delete('Resident Advisor')
  names.delete('Submit')
  names.delete('Listings')
  names.delete('Clubs')
  names.delete('News')
  names.delete('Reviews')
  names.delete('Features')
  names.delete('Films')
  names.delete('Submit event')
  names.delete('Artists')
  names.delete('Photos')
  names.delete('DJ Charts')
  names.delete('Labels')
  names.delete('Podcasts')
  names.delete('Search')
  names.delete('Top 1000')
  names.delete('Top 100')
  names.delete('Local')
  names.delete('Favourites')
  names.delete('Create an artist profile')
  names.delete('Reviews')
  names.delete('Features')
  names.delete('A') 
  names.delete('B') 
  names.delete('C') 
  names.delete('D') 
  names.delete('E') 
  names.delete('F') 
  names.delete('G') 
  names.delete('H') 
  names.delete('I') 
  names.delete('J') 
  names.delete('K') 
  names.delete('L') 
  names.delete('M') 
  names.delete('N') 
  names.delete('O') 
  names.delete('P') 
  names.delete('Q') 
  names.delete('R') 
  names.delete('S') 
  names.delete('T') 
  names.delete('U') 
  names.delete('V') 
  names.delete('W') 
  names.delete('X') 
  names.delete('Y') 
  names.delete('Z') 
  names.delete('0-9') 
  names.delete('RA') 
  names.delete('About') 
  names.delete('Advertise') 
  names.delete('Jobs') 
  names.delete('RA In Residence') 
  names.delete('Ticketing FAQ') 
  names.delete('Sell tickets on RA') 
  names.delete('Privacy') 
  names.delete('Terms') 
  names.delete('RA is also available in Japanese. 日本版') 
  names.delete('Download the RA Guide') 
  names.delete('RA on Twitter') 
  names.delete('RA on Facebook') 
  names.delete('RA on Google+') 
  names.delete('RA on Instagram') 
  names.delete('RA on Soundcloud')
  names.delete('Biography')
  names.delete('Events')
  names.delete('Tracks')
  names.delete('RA News')
  names.delete('RA Editorial')
  names.delete('Remixes')
  names.delete('Solo productions')
  names.delete('Collaborations')
  names.delete('Laboratory Instinct')
  names.delete('Highgrade Records')
  names.delete('Bosconi')
  names.delete('!K7')
  names.delete('Perlon')
  names.delete('Beatstreet')
  names.delete('Title')
  names.delete('Label')
  names.delete('Release Date')
  names.delete('51 chartings')
  puts names
end

#~ To_CSV
for $n in 0..names.count do 
  CSV.open('Most_Charted.csv','a+') do |csv|
    csv  << [names[$n]]

  end
end

That creates a CSV file with:

PositiveNoise (Carl Craig remix)  System 7 & Guy Called Gerald  A-Wave 22 chartings

Voodoo Ray (Shield Re-Edit)   A Guy Called Gerald   18 chartings

Falling (D. Digglers Cleptomania remix)   Tom Clark & Benno Blome feat.
  A Guy Called Gerald   18 chartings

How Long Is Now   A Guy Called Gerald   14 chartings

Groove Of The Ghetto  A Guy Called Gerald   12 chartings

Voodoo Ray  A Guy Called Gerald   10 chartings

Falling (D Diggler's Rescue remix)  Tom Clark & Benno Blome feat. A
Guy Called Gerald   9 chartings

and so on.

How do I pass only the first 5 song names to the CSV file?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Leo F
  • 17
  • 1
  • 2
  • This looks like a very bad idea: `OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE`. Also see [The most dangerous code in the world: validating SSL certificates in non-browser software](http://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html). – jww Jun 18 '16 at 12:40
  • There's a lot wrong with your code: Don't use `$n` (globals) unless you understand why and when to use them. They can cause debugging nightmares. Your long list of `delete`s can be written much more elegantly. – the Tin Man Jun 20 '16 at 20:28

1 Answers1

1

Be sure you know what you are doing when you disable SSL checks.

You can find a better selector for the track list, so you do not need all those "deletes". The tracks are all inside ul.tracks

Then i'd suggest you make the whole thing a class. So you can encapsulate the behavior. And then don't use $ globals. Not needed and usually a sign of bad code.

Here is a working sample:

require "openssl"
require "nokogiri"
require 'csv'
require "open-uri"
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

class Tracklist

  def initialize(url)
    @url = url
  end

  def parse(top = nil)
    html = Nokogiri::HTML(open(url))
    result = []

    html.css('ul.tracks li').each do |node|
      title = node.css('div.title a:nth-child(1)').first
      result << title.text if !title.nil?
      break if top && result.length == top
    end

    result
  end

  private

  attr_reader :url

end


list = Tracklist.new("https://www.residentadvisor.net/dj/aguycalledgerald/tracks?sort=mostcharted")
p list.parse(5)

If you need more information about the tracks, then you can extract more details in the loop inside the parsemethod.

This code stops parsing after top has been reached. Afterwards you can build your CSV as you like.

Pascal
  • 8,464
  • 1
  • 20
  • 31