Scrape from tiprank through google sheet app script

i am using the following google sheet appscript to scrape tiprank for target price.

function TRTP(url) {
const html = UrlFetchApp.fetch(url).getContentText();
const res = html.match(/<div class="flexcb_ bgwhite h12    w12  px0  displayflex positionrelative py3">.+?(<text .+?</text>)/);
if (!res) throw new Error("Value cannot be retrieved.")
return res;
}

I am using the following URL: https://www.tipranks.com/stocks/msft/forecast

This is trying to pull data from the following snapshot from URL above.

Targetprice from Tiprank

This shoud give me a long OuterHTML in which values are also present .. i use google sheet formula to extract target price from that.

This work on other websites like WSJ but is not working on Tiprank. can anyone help me on this.

Another solution, not so elegant

function TRTP(url) {
const html = UrlFetchApp.fetch(url).getContentText();
var res = html;
res = res.match(/(?<=flexccc    mt3 displayflex colorpale shrink0 lineHeight2 fontSize2 ml2 ipad_fontSize3.*>).*/g)[0].substring(0,50);
res = res.match(/.*(?=<)/g)[0];
if (!res) throw new Error("Value cannot be retrieved.")
return res;
}

Try this

=importxml("https://www.tipranks.com/stocks/msft/forecast","//div[@class="flexccc    mt3 displayflex colorpale shrink0 lineHeight2 fontSize2 ml2 ipad_fontSize3"]")

Leave a Comment