Reputation:
I'm currently trying to scrape data using Nokogiri from a webpage. So far, I have been able to successfully scrape title and price information using this code in the controller:
@items = doc.xpath("//div[contains(@class, 'name')]/a").collect {|node| node.text.strip}
@prices = doc.xpath("//div[contains(@class, 'price')]/span[contains(@class, 'price-new')]").collect {|node| node.text.strip}
and this code in the view:
<% @items.zip(@prices).each do |title,price| %>
<%= title+" "+price%>
<% end %>
But this code for images won't work. (Note that I'm trying to scrape the <img>
tag):
@images = doc.xpath("//div[contains(@class, 'image')]/a/img").collect
Anything I try to write into the view .erb just comes back with a syntax error. Any idea what I'm missing or should be using in the .erb?
Upvotes: 1
Views: 147
Reputation: 1622
Is there a typo there? This:
@images = doc.xpath("//div[contains(@class, 'image')]/a/img").collect
Will just return an Enumerator
. If that's not a typo then the answer would be that you need to pass a block to collect
, maybe:
@images = doc.xpath("//div[contains(@class, 'image')]/a/img").collect { |element| element.attribute("src") }
It's not clear what you're doing next with @images
but this would get you the URLs of each image so you can display them yourself.
Upvotes: 1