The task was to generate a listing of active FINRA registrants for a particular FI form FINRAs BrokerCheck website and once again some Ruby script with a Watir and Nokogiri Gem was the goto.
require 'nokogiri' require 'rubygems' require 'watir-webdriver'
rows = Array.new dupes = Array.new
### Defines Array Class for HTML Table output ### class Array def to_cells(tag) self.map { |c| "<#{tag}>#{c}</#{tag}>" }.join end end browser = Watir::Browser.new :chrome browser.goto 'http://brokercheck.finra.org/Search/Search.aspx'
[*('A'..'Z')].each do |letter| browser.text_field(:name => 'ctl00$phContent$ucUnifiedSearch$txtIndvl').set "#{letter}" + "*" browser.text_field(:name => 'ctl00$phContent$ucUnifiedSearch$txtFirm').set 'BMO' browser.input(:name => 'ctl00$phContent$ucUnifiedSearch$lbtnFreeFormSearch').click
loop do
table_Rows = browser.table(:id, 'ctl00_phContent_gvBrokerTable').rows.length
for i in 0..table_Rows.to_i-1 doc = Nokogiri::HTML.parse(browser.html) name = browser.table(:id, 'ctl00_phContent_gvBrokerTable')[i][0].div(:class, 'gvListItemStyle').span.text lic_status = browser.table(:id, 'ctl00_phContent_gvBrokerTable')[i][1].text status = browser.table(:id, 'ctl00_phContent_gvBrokerTable')[i][0].div(:class, 'gvListItemStyle').text if lic_status.to_s =~ /Not Licensed/
elsif status.to_s =~ /BMO/ source = doc.css('.GrayTextShade:nth-child(3)')[i].text if dupes.include?(name) else puts "#{name}\t#{source}" rows << {"Name" => name, "Registration" => source} dupes << name end
end
end
if browser.link(:id =>'ctl00_phContent_navPager_lbNext').exists?
browser.link(:id =>'ctl00_phContent_navPager_lbNext').click else browser.goto 'http://brokercheck.finra.org/Search/Search.aspx' break
end
end
end
### Rolls HTML Table output ### headers = "<tr>#{rows[0].keys.to_cells('th')}</tr>" cells = rows.map do |row| "<tr>#{row.values.to_cells('td')}</tr>" end.join("\n ") table = "<table border=\"1\"> #{headers} #{cells} </table>" puts table