-2

I've been sent a worked example by a friend but unfortunately i'm really struggling with it and can't seem to work out how to pull the information into a dataframe to then manipulate it.

I initially thought I would be able to simply parse it using JsonLite but this is proving trickier than expected. Can someone help me out here? Would be much appreciated! If no one is willing to write the code for me, I'd really appreciate someone talking me through what this link actually means!

The URL provided is : smarkets.herokuapp.com/bets/{betID}

Jim White
  • 13
  • 3
  • This is really a bad question. At least you should state clear what are you struggling with, what is not working, etc. – Feng Jiang Jun 12 '18 at 17:45

1 Answers1

0

I tried id 123, and the response structure doesn't seem to be complex:

library(jsonlite)
library(httr)

#convert response into string
my_url <- "http://smarkets.herokuapp.com/bets/123"
response <- GET(my_url)
text <- content(response,as = "text")

# string to list
my_list <- fromJSON(text) 

#list to dataframe
mydf <- data.frame(t(sapply(my_list,c)))

Sample result:

> mydf
id user_id   amount percentage_odds                      timestamp result
1 123     421 5.729698              16 2018-06-12T15:49:13.245956399Z  FALSE

Make a loop to get all info:

library(jsonlite)
library(httr)
library(glue)

#convert response into string
loop_items <- 1:1000

all_df <- data.frame()
for( i in 1 : 1000){
    ua <- user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36")
    my_url <- glue("http://smarkets.herokuapp.com/bets/{i}",
                   i = i)
    response <- GET(my_url,ua)
    text <- content(response,as = "text")
    # string to list
    my_list <- fromJSON(text) 
    #list to dataframe
    mydf <- data.frame(t(sapply(my_list,c)))
    all_df <- rbind(all_df,mydf)
    #sleep 1 second
    Sys.sleep(1)
    cat(i," completed\n")
}
yusuzech
  • 5,896
  • 1
  • 18
  • 33