Eric Eric - 5 months ago 9
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", "cool.media", "Peek", "KLIKsee", "Streamingo Solutions", "Pvgna", "alugha", "User"]


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

The HTML structure looks like this

<div class='name'>
<a href="https://angel.co/mikegetsleads-2" class="startup-link" data-id="1217822" data-type="Startup">MikeGetsLeads</a>
</div>

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

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

pp names

returns:

["MikeGetsLeads", "MikeGetsLeads2", "MikeGetsLeads3"]