Skip to content

Instantly share code, notes, and snippets.

@spceaza
Last active May 23, 2019 03:27
Show Gist options
  • Save spceaza/2f7a651edf02a084d507b9107ecdead7 to your computer and use it in GitHub Desktop.
Save spceaza/2f7a651edf02a084d507b9107ecdead7 to your computer and use it in GitHub Desktop.
base_url = "https://etfdb.com/data_set/?tm=40284&cond={%22by_etf%22:1272}&no_null_sort=true&count_by_id=&sort=weight&order=desc&limit=15&offset="
CleanString = function( string )
{
result = gsub( "<a.*stock/", "", string )
gsub( "/.*", "", result )
}
WeightsFromUrl = function( json_url )
{
json_data = jsonlite::read_json( json_url )
weights_data = sapply( json_data$rows , function( x ) x$weight )
holdings_data = sapply( json_data$rows , function( x ) CleanString( x$holding ) )
data.frame(weights_data, holdings_data)
}
lapply( paste0( base_url, ( 0:2 ) * 15 ), WeightsFromUrl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment