A
A
Alexandr Mi2018-03-15 09:37:46
JavaScript
Alexandr Mi, 2018-03-15 09:37:46

How to get data from js variable using ruby?

There is an HTML file, in the body of which there is a js script, which in turn has a variable (highlighted in the photo below) that stores an array of objects.
5aaa119c885df624028854.png
The task is to get this HTML by reference using Ruby, and then somehow get to the treasured variable and get the array stored in it and load it into the Ruby variable for further work.
What would you recommend to use to get HTML data in Ruby?
What is the best way to get to this variable (by searching for a string by the name of the variable? Or are there any other options?)
In general, maybe someone came across such a task, help than you can ^__^

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RaMzz, 2018-03-18
@RaMzz

And here is an article on how to parse in Ruby, this can be done with whores and blackjack: D
https://habrahabr.ru/post/51610/

S
Sergey Blokhin, 2018-03-21
@TITnet

For example using Selenium WebDriver.

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.get 'https://ya.ru'
result = driver.execute_script 'return Ya._metrika.hitId;'
puts result

The example above will get the `JavaScript` value of the `Ya._metrika.hitId` variable created on the ya.ru page.
In your case, `attributesCombinations` needs to be retrieved.
Another option is to use a JavaScript parser.
For example, https://github.com/tenderlove/rkelly or any similar one.
Get the JavaScript source code from the page and feed it to the parser.
Getting the code is easy enough.
require 'unirest'
require 'nokogiri'
response = Unirest.get 'http://example.com'
doc = Nokogiri::HTML response.raw_body
element = doc.at_css 'script' # тут нужен более точный селектор
element.text # вернёт вам сырой JavaScript

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question