I am scraping a webpage for text, using the code as follows
doc.xpath("//td[text()='Operating system']/following-sibling::td")
doc.xpath("//td[text()='Processors']/following-sibling::td")
I have about 30 of these so I thought I could maybe use an array but its not working, here is my code
clues = Array.new
clues << 'Operating system'
clues << 'Processors'
clues << 'Chipset'
clues.each do |clue_storeage|
doc.xpath("//td[text()=#{clues}]/following-sibling::td")
end
Is there a way I can feed the array into that loop and then output that to a CSV?
To clarify mb2nd’s comment, your each block is incorrectly referencing the array. This should work:
To output the captured data to CSV you could run:
The doc.xpath(“//td[text()=#{clue}]/following-sibling::td”) call may need .value on the end?
On a side note; you may also populate your array like so:
EDIT (after last comment from @Ninja2K)
You need to save the result of each the xpath calls. Here’s some working code:
BTW. You may also find this article useful: http://hunterpowers.com/data-scraping-and-more-with-ruby-nokogiri-sinatra-and-heroku/