uri = URI('https://www.random.org/dice/?num=10')
I'm using the above code to call this random dice generator.
I'm able to get the entire http response using the get method above.
But I'm stuck on how to access the result from that big HTML string?
I'm just trying to access the individual dice values (10 in this case).
Absolutely! I just found Nokogiri, made a simple little repl, pulling out the HTML from the page.
First, I did an Inspect on the dice rolling page using Chrome's developer tools to see that the dice can be selected from
#invisible p img. There's no other good classes or ids to attach to because the HTML is loosely typed after the
Then, using Nokogiri, I grab the HTML of the page, create a simple for loop going from 0-9 (for 10 dice), and output to the console the results of grabbing the element at
i position with the selector
#invisible p img and the
alt attribute which correlates to the rolled number.
It ends up becoming 13 lines of code.
require 'nokogiri' require 'rubygems' require 'open-uri' page = Nokogiri::HTML(open("https://www.random.org/dice/?num=10")) i = 0 loop do i += 1 if i == 10 break end puts page.css("#invisible p img")[i]["alt"] end
If this solves your problem, please consider upvoting and marking my answer as the accepted to close this question. I will still continue to answer questions.
@deeptishukla24 I think there is a check mark below the upvote icon in the top comment I made with the answer. I think you have to put your mouse over it for it to show up.
I'm referring back to this page on repl: https://repl.it/talk/announcements/Mark-your-question-as-heavy-check-markanswered-on-Repl-Talk/13187?order=new
Why are you even trying to get this response? Are you just trying to get a random dice roll?
@heyitsmarcus thanks for responding. I was asked this question in a recent programming interview. The question was to call this uri, get individual dice values and print to stdout. But I was stuck on the parsing part.
If it was a normal JSON response, I'd have used JSON.parse(response.body).
I'm also trying to see if I can use httparty to get this to work.