Skip to content

Instantly share code, notes, and snippets.

@axlevisu
Last active December 16, 2015 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axlevisu/093996a5001708cc8d0a to your computer and use it in GitHub Desktop.
Save axlevisu/093996a5001708cc8d0a to your computer and use it in GitHub Desktop.
Code to get money_control_ids of companies from their website. Input data should be BSE_code/NSE_code/ISIN_code. I used ISIN_code data.
library(httr)
#library(stringr) #for str_split which is same as strsplit
data <- read.csv("nse.csv",header= TRUE,sep=",",stringsAsFactors = FALSE)
search_url <- "http://www.moneycontrol.com/stocks/cptmarket/compsearchnew.php?search_str="
url_list = paste(search_url,data[,'ISIN.code'],sep = "")
mc_list =c()
for(url in url_list){
x <- GET(url)
#redirect_url <- unlist(strsplit(x$url,"/"))
#mc_id<- redirect_url[length(redirect_url)]
redirect_url <- as.vector(unlist(strsplit(x$url,"")))
l <- length(redirect_url)
mc_id <-paste(redirect_url[seq(2+l-match('/',rev(redirect_url)),l)],collapse ="")
mc_list <- c(mc_list,mc_id)
print(mc_id)
}
data['moneycontrol.id'] <-mc_list
write.csv(file = "withmcids.csv", x=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment