-1

Im trying to get the price generically from all the shopping sites

What i tried so far :

            price = doc.xpath('//span[contains(text(), "$")]').try(:first).try(:content)

            if (!price)
                price = doc.xpath('//div[contains(text(), "$")]').try(:first).try(:content)
            end

Example of the html i tried : https://jet.com/product/adidas-Real-Madrid-Ball-15-5/a08713c229924ceca7171850680d3e32 (The HTML of this url)

this is not working so well, what am i doing wrong? Thanks you all

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
R.Bar
  • 362
  • 1
  • 16
  • Welcome to Stack Overflow. Read "[ask]" and "[mcve]". You need to supply a minimal example of the HTML in the question. What does "this is not working so well" mean? Errors? Failure to get any result? It burned off the atmosphere? – the Tin Man Mar 21 '16 at 23:40

1 Answers1

0

You may try

Nokogiri: How to select nodes by matching text?

I've change a little and it worked

require 'pp'
require 'nokogiri'

html = '
<html>
<body>
  <p>foo</p>
  <p>$bar</p>
</body>
</html>
'

doc = Nokogiri::HTML(html)
pp doc.at('p:contains("$")').text.strip
Community
  • 1
  • 1
devanand
  • 5,116
  • 2
  • 20
  • 19
  • Hey, first of all thanks you. The problem here is that i dont know which element i search for. in some sites its can be a div in other its can be a span. What can i do ? Thanks – R.Bar Mar 19 '16 at 15:38
  • Spidering/scraping involves intimate knowledge of the pages. There is no generic, one-size-fits-all code that can accurately navigate or scrape a page. In other words, you are going to have to figure out what to search for. That's why programming is hard, you have to figure out how to tell the computer what to do. – the Tin Man Mar 31 '16 at 18:26