Guest User

Yahoo Data Scraping

a guest
Jun 27th, 2022
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function yahooData(code,percent,flat,C1,C2,C3,C4) {
  2.   var url='https://finance.yahoo.com/quote/'+code+'/key-statistics?p='+code
  3.   var source = UrlFetchApp.fetch(url).getContentText()
  4.   var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
  5.   var data = JSON.parse(jsonString)
  6.   var result = []
  7.  
  8.   // Get the trailing PE
  9.  
  10.   let TPEe = 0
  11.   let TPE1 = 0
  12.   let TPE2 = data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.trailingPE.fmt
  13.   try{TPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPeRatio[0].reportedValue.fmt}
  14.   catch(e) {TPE1 = 10000;TPEe = TPEe+1}
  15.  
  16.   if (Math.abs(TPE1-TPE2)<(TPE2/percent+flat)) {result.push(TPE1)}
  17.   else {
  18.     try{TPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPeRatio[1].reportedValue.fmt}
  19.     catch(e) {TPE1 = 10000}
  20.     if (Math.abs(TPE1-TPE2)<(TPE2/percent+flat)) {result.push(TPE1)}
  21.     else if (C1 == 1) {result.push(TPE2);TPEe = TPEe+1}
  22.     else {result.push('');TPEe = TPEe+1}
  23.   }
  24.  
  25.   // Get the forward PE
  26.  
  27.   let FPEe = 0
  28.   let FPE1 = 0
  29.   let FPE2 = data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.forwardPE.fmt
  30.   try{FPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingForwardPeRatio[0].reportedValue.fmt}
  31.   catch(e){FPE1 = 10000;FPEe = FPEe+1}
  32.  
  33.   if (Math.abs(FPE1-FPE2)<(FPE2/percent+flat)) {result.push(FPE1)}
  34.   else {
  35.     try{FPE1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingForwardPeRatio[1].reportedValue.fmt}
  36.     catch(e) {FPE1 = 10000}
  37.     if (Math.abs(FPE1-FPE2)<(FPE2/percent+flat)) {result.push(FPE1)}
  38.     else if (C2 == 1) {result.push(FPE2);FPEe = FPEe+1}
  39.     else {result.push('');FPEe = FPEe+1}
  40.   }
  41.  
  42.   // Get the PEG Ratio
  43.  
  44.   let PEGe = 0
  45.   let PEG1 = 0
  46.   let PEG2 = data.context.dispatcher.stores.QuoteSummaryStore.defaultKeyStatistics.pegRatio.fmt
  47.   try{PEG1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPegRatio[0].reportedValue.fmt}
  48.   catch(e){PEG1 = 10000;PEGe = PEGe+1}
  49.  
  50.   if (Math.abs(PEG1-PEG2)<(PEG2/percent+flat*2)) {result.push(PEG1)}
  51.   else {
  52.     try{PEG1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPegRatio[1].reportedValue.fmt}
  53.     catch(e) {PEG1 = 10000}
  54.     if (Math.abs(PEG1-PEG2)<(PEG2/percent+flat*2)) {result.push(PEG1)}
  55.     else if (C3 == 1) {result.push(PEG2);PEGe = PEGe+1}
  56.     else {result.push('');PEGe = PEGe+1}
  57.   }
  58.  
  59.   // Get the P/B Ratio
  60.  
  61.   let PBe = 0
  62.   let PB1 = 0
  63.   let PB2 = data.context.dispatcher.stores.QuoteSummaryStore.defaultKeyStatistics.priceToBook.fmt
  64.   try{PB1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPbRatio[0].reportedValue.fmt}
  65.   catch(e){PB1 = 10000;PBe = PBe+1}
  66.  
  67.   if (Math.abs(PB1-PB2)<(PB2/percent+flat)) {result.push(PB1)}
  68.   else {
  69.     try{PB1 = data.context.dispatcher.stores.QuoteTimeSeriesStore.timeSeries.trailingPbRatio[1].reportedValue.fmt}
  70.     catch(e) {PB1 = 10000}
  71.     if (Math.abs(PB1-PB2)<(PB2/percent+flat)) {result.push(PB1)}
  72.     else if (C4 == 1) {result.push(PB2);PBe = PBe+1}
  73.     else {result.push('');PBe = PBe+1}
  74.   }
  75.  
  76.   // Get the price values
  77.  
  78.   try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.currentPrice.fmt)}
  79.   catch(e){result.push('not found')}
  80.   try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetLowPrice.fmt)}
  81.   catch(e){result.push('not found')}
  82.   try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetMeanPrice.fmt)}
  83.   catch(e){result.push('not found')}
  84.   try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.financialData.targetHighPrice.fmt)}
  85.   catch(e){result.push('not found')}
  86.   try{result.push(data.context.dispatcher.stores.QuoteSummaryStore.summaryDetail.fiftyTwoWeekHigh.fmt)}
  87.   catch(e){result.push('not found')}
  88.  
  89.   // Push the fundamental state
  90.  
  91.   result.push(TPEe)
  92.   result.push(FPEe)
  93.   result.push(PEGe)
  94.   result.push(PBe)
  95.  
  96.   return [result]
  97. }
Advertisement
Add Comment
Please, Sign In to add comment