Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:36:54+00:00 2026-05-26T14:36:54+00:00

I’m an amateur programmer wanting to scrape data from a site that is similar

  • 0

I’m an amateur programmer wanting to scrape data from a site that is similar to this site: http://www.highschoolsports.net/massey/ (I have permission to scrape the site, by the way.)

The target site has ‘th’ classes for each ‘th’ in row[0] but I want to ensure that each ‘TD’ I pull from each table is somehow linked to that th’s class name, because the tables are inconsistent, for example one table might be:

row[0] – >>th.name, th.place, th.team

row[1] – >>td[0], td[1] , td[2]

while another might be:

row[0] – >>th.place, th.team, th.name

row[1] – >>td[0], td[1] , td[2] etc..

My Question: How do I capture the ‘th’ class name across many hundreds of tables which are inconsistent(in ‘th’ class order) and create the 10-14 variables(arrays), then link the ‘td’ corresponding to that column in the table to that dynamic variable? Please let me know if this is confusing.. there are multiple tables on a given page..

Currently my code is something like:

require 'rubygems'
require 'mechanize'
require 'nokogiri'
require 'uri'

class Result

  def initialize(row)
    @attrs = {}
    @attrs[:raw] = row.text
  end

end

class Race

  def initialize(page, table)
    @handle = page
    @table = table
    @results = []
    @attrs = {}
    parse!
  end

  def parse!
    @attrs[:name] = @handle.css('div.caption').text
    get_rows

  end

  def get_rows
    # get all of the rows .. 
    @handle.css('tr').each do |tr|
      @results << RaceResult.new(tr)
    end
  end

end

class Event

  class << self

    def all(index_url)
      events = []
      ourl = Nokogiri::HTML(open(index_url))
      ourl.css('a.event').each do |url|
        abs_url = MAIN + url.attributes["href"]
        events << Event.new(abs_url)
      end
      events
    end

  end

  def initialize(url)
    @url = url
    @handle = nil
    @attrs = {}
    @races = []
    @sub_events = []
    parse!
  end

  def parse!
    @handle = Nokogiri::HTML(open(@url))
    get_page_meta
    if(@handle.css('table.base.event_results').length > 0)
      @handle.search('div.table_container.event_results').each do |table|
        @races << Race.new(@handle, table)
      end
    else
      @handle.css('div.centered a.obvious').each do |ol|
        @sub_events << Event.new(BASE_URL + ol.attributes["href"])
      end
    end
  end

  def get_page_meta
    @attrs[:name] = @handle.css('html body div.content h2 text()')[0] # event name
    @attrs[:date] = @handle.xpath("html/body/div/div/text()[2]").text.strip #date
  end

end

A friend has been helping me with this and I’m just starting to get a grasp on OOP but I’m only capture the tables and they’re not split into td’s and stored into some kind of variable/array/hash etc.. I need help understanding this process or how to do this. The critical piece would be dynamically assigning variable names according to the classes of the data and moving the ‘td’s’ from that column (all td[2]’s for example) into that dynamic variable name. I can’t tell you how amazing it would be if someone actually could help me solve this problem and understand how to make this work. Thank you in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:36:55+00:00Added an answer on May 26, 2026 at 2:36 pm

    It’s easy once you realize that the th contents are the keys of your hash. Example:

    @items = []
    doc.css('table.masseyStyleTable').each do |table|
        fields = table.css('th').map{|x| x.text.strip}
        table.css('tr').each do |tr|
            item = {}
            fields.each_with_index do |field,i|
                item[field] = tr.css('td')[i].text.strip rescue ''
            end
            @items << item      
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.