Eric Eric - 1 year ago 60
Ruby Question

Properly separate String elements in an Array

I am trying to parse an HTML page using Nokogiri to get some companies names.

names = []
names << Nokogiri::HTML(mypage).css(".name a").text

My result is:

["MikeGetsLeadsUruBlondeLaunch LIVERynoRyderBoyer ProductionsStrangerxCerealLume CubeKatapyMacaulay Outdoor PromotionsFlixit ABMedia MosaicLiftCast.TVcool.mediaPeekKLIKseeStreamingo SolutionsPvgnaalughaUser"]

But what I'd like to get is:

["MikeGetsLeads", "Uru", "Blonde", "Launch LIVE", RynoRyderBoyer Productions", "Stranger", "xCereal", "Lume Cube", "Katapy", "Macaulay Outdoor Promotions", "Flixit AB", "Media Mosaic", "LiftCast.TV", "", "Peek", "KLIKsee", "Streamingo Solutions", "Pvgna", "alugha", "User"]

I tried to use
but it does not give me the right result neither. On this page, each name belongs to a
so it's clearly separated in the HTML structure.

The HTML structure looks like this

<div class='name'>
<a href="" class="startup-link" data-id="1217822" data-type="Startup">MikeGetsLeads</a>

Answer Source
require 'rubygems'
require 'nokogiri'
require 'pp'

names = []
mypage ="myhtml.html", "r")
Nokogiri::HTML(mypage).css(".name a").each do |item|
 names << item.text

pp names


["MikeGetsLeads", "MikeGetsLeads2", "MikeGetsLeads3"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download