-1

I have an rails application wherein i am scraping data from the internet. I have this snippet of code where it reports syntax errors thus preventing it from running.

I have tried to sort it out but unable to find out what is wrong. Where am i going wrong.

The snippet is shown below:

def reuters
    ticker_sym = 'FB.O'
    reuters_home_url = "http://in.reuters.com"
    reuters_base_url = "http://in.reuters.com/finance/stocks/"
    board_members =  Nokogiri::HTML(open(reuters_base_url + 'companyOfficers?symbol=' + ticker_sym.to_s ))
    members = []
    table = board_members.css('.column1 tbody.dataSmall').first
    table_desc = board_members.css('.column1 tbody.dataSmall')[1]
    table.css('tr').each_with_index do |row,index|
      next if index == 0
      members << {
          name: row.css('td[1] h2 a').text.strip,
          title: row.css('td[4]').text.strip,
          position_held: row.css('td[3]').text.strip,
          age: row.css('td[2]').text.strip,
          member_link: URI.join(reuters_home_url,row.css('td[1] h2 a').attr("href")).to_s
          table_desc.css('tr').each_with_index do |col,index2|
            next if index2 == 0
            members << {
                description: col.css('td[2]').text.strip
            }
          end
        }
    end
  end

Have attached a screenshot of my rails application error page shown below:

Rails error page

Ahkshaey Ravi
  • 105
  • 1
  • 8
  • Don't do things like `row.css('td[1] h2 a').text`. Use `at` or `at_css` instead of `css`. Your future self will thank you. – the Tin Man Jan 19 '17 at 18:12
  • what could be a better way to access the data.can you show me using my example for brevity sake. I'm totally new to nokogiri – Ahkshaey Ravi Jan 19 '17 at 18:39
  • The basic problem is that the `text` method for a NodeSet will concatenate all the text together usually resulting in very messy output. Instead you want to use it only on a Node. http://stackoverflow.com/a/39477538/128421 – the Tin Man Jan 19 '17 at 20:15

1 Answers1

0

Add } before table_desc.css('tr').each_with_index do |col, index2| and remove } after end like this.

def reuters
  ticker_sym = 'FB.O'
  reuters_home_url = "http://in.reuters.com"
  reuters_base_url = "http://in.reuters.com/finance/stocks/"
  board_members = Nokogiri::HTML(open(reuters_base_url + 'companyOfficers?symbol=' + ticker_sym.to_s))
  members = []
  table = board_members.css('.column1 tbody.dataSmall').first
  table_desc = board_members.css('.column1 tbody.dataSmall')[1]
  table.css('tr').each_with_index do |row, index|
    next if index == 0
    members << {
      name: row.css('td[1] h2 a').text.strip,
      title: row.css('td[4]').text.strip,
      position_held: row.css('td[3]').text.strip,
      age: row.css('td[2]').text.strip,
      member_link: URI.join(reuters_home_url, row.css('td[1] h2 a').attr("href")).to_s
    }
    table_desc.css('tr').each_with_index do |col, index2|
      next if index2 == 0
      members << {
        description: col.css('td[2]').text.strip
      }
    end
  end
end
Lukas Baliak
  • 2,849
  • 2
  • 23
  • 26
  • The syntax error problem was solved but that doesn't give me the data in the right structure. I want the table_desc data to be looped inside the outer loop. basically I want to store the table_desc data as a key value pair in the very first loop itself. – Ahkshaey Ravi Jan 19 '17 at 15:18
  • @AhkshaeyRavi your question is about Sintax error ;) Create new question with all data. You can use this for help: http://stackoverflow.com/help/how-to-ask – Lukas Baliak Jan 19 '17 at 15:20
  • all I want is to loop both 'table' and 'table_desc' nodesets in a single go so that i get all the necessary info in a single hash like structure. – Ahkshaey Ravi Jan 19 '17 at 15:25