Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert a html table to CSV using ruby
# run using ```rvm jruby-1.6.7 do jruby "-J-Xmx2000m" "--1.9" tej.rb```
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("/tmp/preview.html")
doc = Nokogiri::HTML(f)
csv = CSV.open("/tmp/output.csv", 'w',{:col_sep => ",", :quote_char => '\'', :force_quotes => true})
#doc.xpath('//table/tbody/tr').take(10).each do |row|
doc.xpath('//table/tbody/tr').each do |row|
tarray = []
row.xpath('td').each do |cell|
tarray << cell.text
end
csv << tarray
end
csv.close
@mejibyte

This comment has been minimized.

Copy link

mejibyte commented Jun 22, 2013

This saved me some time. Thanks!

@tkt028

This comment has been minimized.

Copy link

tkt028 commented Jul 13, 2014

Thanks! It works for me. :D

@G-Square

This comment has been minimized.

Copy link

G-Square commented Oct 21, 2015

thanks

@isorsa

This comment has been minimized.

Copy link

isorsa commented Jan 20, 2016

Thanks!

@mahendhar9

This comment has been minimized.

Copy link

mahendhar9 commented Jul 13, 2016

Many Thanks!

@shrishti01

This comment has been minimized.

Copy link

shrishti01 commented Nov 22, 2017

Hi can anyone provide w with the script which convert full html file in csv file including tables and text

@debazav

This comment has been minimized.

Copy link

debazav commented May 18, 2018

THANKS!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.