0

Not quite sure what I should do at this point..I am utilizing a regular expression to capture JSON within the HTML on a website. I've initialized a for loop to go through everyline in the array to find {"page_cur":

I've attempted to push it to an array with the following code.
line.push(json_text)
The result was the entire sites source code. What am I doing wrong?

require 'mechanize'
require 'json'

mechanize = Mechanize.new

url = mechanize.get('http://www.hypem.com/')
page = Array.new
page = url.body.split(/\n/) 
json_text = Array.new

#look through every line of code
for line in page 

    #find {"page_cur": 
    next unless line =~ /^\s*\{"page_cur":/  
        #delete </script> tag on the last line
        line.sub! /<.script>/, '' 
        #push into array?

end
3
  • you need () to capture things...
    – Marc B
    Commented Apr 16, 2015 at 20:22
  • where is this on the page. Your best bet is to navigate to the location on the page then parse the JSON. Also next if means skip the line if it matches this pattern which is currently from the start of a string with an indeterminate amount of spaces then \{"page_cur": Commented Apr 16, 2015 at 20:50
  • I was trying to use next unless after I saw your comment and changed the operator from == to =~ Thank you all for the help.
    – crushedhat
    Commented Apr 16, 2015 at 21:06

1 Answer 1

0

If you are trying to push into the json_text array it should be json_text.push(line)

2
  • Well it definitely pushed line into json_text, but it pushed the entire source code and not the snippet of JSON.
    – crushedhat
    Commented Apr 16, 2015 at 20:44
  • Or just json_text << line but either way if the given page is correct this does not exist anywhere in the page body. Commented Apr 16, 2015 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.