Newtt
Newtt

Reputation: 6190

DOM Scraping using nokogiri

I've got a Ruby script for DOM Scraping running Nokogiri gem as follows:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

names = Array.new
contact = Array.new
address = Array.new
final = Array.new
doc = Nokogiri::HTML(open('INSERTURLHERE')) 

names = doc.xpath("//span[contains(@class,'jcn')]/a/text()").collect {|node| node.strip.text}
contact = doc.xpath("//p[contains(@class,'jrcw')]/a/b/text()").collect {|node| node.text.strip}
address = doc.xpath("//p[contains(@class,'jaid')]/text()").collect {|node| node.text.strip}


final.zip(names, contact, address).each do |names, contact, address|
    puts names + " " + contact + " " + address
end  

I used command line to run it as

ruby filename.rb > output.txt

However, the output.txt is empty. Is there something wrong with the script that's causing it to not run?

Upvotes: 0

Views: 120

Answers (1)

falsetru
falsetru

Reputation: 369074

final is an empty array. zipping it with other arrays produces empty array:

final = Array.new
final.zip([1], [2]) # <---
# => []

[1].zip([2])
# => [[1, 2]]

Replace:

final.zip(names, contact, address)

with

names.zip(contact, address)

Upvotes: 1

Related Questions