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 8324229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:59:13+00:00 2026-06-08T23:59:13+00:00

I have a Xpath query which accepts array elements for output using Axslx, I

  • 0

I have a Xpath query which accepts array elements for output using Axslx, I need to tidy up my ouput for certain conditions one of which is the ‘Software included’

My xpath scrapes the following URL http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1

A sample of my code is below:

clues = Array.new
clues << 'Optical drive'
clues << 'Pointing device'
clues << 'Software included'

selector = "//td[text()='%s']/following-sibling::td"

data = clues.map do |clue| 
         xpath = selector % clue
         [clue, doc.at(xpath).text.strip]
       end

Axlsx::Package.new do |p|
  p.workbook.add_worksheet do |sheet|
    data.each { |datum| sheet.add_row datum }
  end
  p.serialize 'output.xlsx'
end

My Current output formatting

enter image description here

My Desired output formatting

enter image description here

  • 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-06-08T23:59:17+00:00Added an answer on June 8, 2026 at 11:59 pm

    If you can rely on the data always using ‘;’ for separators, have a go at this:

    data = []
    clues.each do |clue|
      xpath = selector % clue
      details = doc.at(xpath).text.strip.split(';')
      data << [clue, details.pop]
      details.each { |detail| data << ['', detail] }
    end
    

    to generate the data before the Axlsx::Package.new block

    In answer to you comment/question: You do it with something like this 😉

    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'axlsx'
    
    class Scraper
    
       def initialize(url, selector)
         @url = url
         @selector = selector
       end
    
       def hooks
         @hooks ||= {}
       end
    
       def add_hook(clue, p_roc)
         hooks[clue] = p_roc
       end
    
       def export(file_name)
         Scraper.clues.each do |clue|
           if detail = parse_clue(clue)
             output << [clue, detail.pop]
             detail.each { |datum| output << ['', datum] }
           end
         end
         serialize(file_name)
       end
    
       private
    
       def self.clues
         @clues ||= ['Operating system', 'Processors', 'Chipset', 'Memory type', 'Hard drive', 'Graphics',
                     'Ports', 'Webcam', 'Pointing device', 'Keyboard', 'Network interface', 'Chipset', 'Wireless',
                     'Power supply type', 'Energy efficiency', 'Weight', 'Minimum dimensions (W x D x H)',
                     'Warranty', 'Software included', 'Product color']
       end
    
       def doc
         @doc ||= begin 
                    Nokogiri::HTML(open(@url))
                  rescue
                    raise ArgumentError, 'Invalid URL - Nothing to parse'
                  end
       end
    
       def output
         @output ||= []
       end
    
       def selector_for_clue(clue)
         @selector % clue
       end
    
       def parse_clue(clue)
         if element = doc.at(selector_for_clue(clue))
           call_hook(clue, element) || element.inner_html.split('<br>').each(&:strip)
         end
       end
    
       def call_hook(clue, element)
         if hooks[clue].is_a? Proc
            value = hooks[clue].call(element)
            value.is_a?(Array) ? value : [value]
         end
       end
    
       def package
         @package ||= Axlsx::Package.new
       end
    
       def serialize(file_name)
         package.workbook.add_worksheet do |sheet|
           output.each { |datum| sheet.add_row datum }
         end
         package.serialize(file_name)
       end
    end
    
    scraper = Scraper.new("http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1", "//td[text()='%s']/following-sibling::td")
    
    # define a custom action to take against any elements found.
    os_parse = Proc.new do |element|
      element.inner_html.split('<br>').each(&:strip!).each(&:upcase!)
    end
    
    scraper.add_hook('Operating system', os_parse)
    
    scraper.export('foo.xlsx')
    

    And the FINAL answer is… a gem.

    http://rubydoc.info/gems/ninja2k/0.0.2/frames

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following xpath query which seems to be working but I just
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
I have a problem finding elements in XPath that's contains a certain string ignoring
I have some XML and an XPath query. I'm using Yahoo! widgets, so I'm
I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
I have the below xpath expression //div[@class=post-content]//img which runs on a html page, scanning
I have an XML document that includes XPath expressions that I need to use
If I have an XPath query like NodeA/NodeB[@WIDTH and not(@WIDTH=20)] | NodeC[@WIDTH and not(@WIDTH=20)]/NodeD
I have a set S of small trees S[i] which I need to find
I have an XML, part of it which I want to query is: <c14>06</c14>

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.