Home > front end >  Scrape web page from Google Apps Script
Scrape web page from Google Apps Script

Time:12-09

To get the price from:

<fin-streamer  data-symbol="LU2197908721.SG" data-test="qsp-price" data-field="regularMarketPrice" data-trend="none" data-pricehint="2" value="115.24" active="">115.24</fin-streamer>

I'm using:

contentText.match(/<fin-streamer(?:.*?)active="">(\d [,]?[\d\.] ?)<\/fin-streamer>/); 

And this is working well.

Now, to obtain the value (0.0066386657) from:

<fin-streamer  data-symbol="LU2197908721.SG" data-field="regularMarketChangePercent" data-trend="txt" data-pricehint="2" data-template="({fmt})" value="0.0066386657" active=""><span >( 0.66%)</span></fin-streamer>

I'm trying with several regex expressions within:

contentText.match(/<fin-streamer(?:.*?)active="">   ???   <\/fin-streamer>/); 

Can anyone help?

Thanks in advance!

CodePudding user response:

You can use the data-field and value attributes as a guide. These worked for me, one for your first sample and one for the second:

/<fin-streamer(?:.*?)(?:regularMarketPrice)(?:.*?)(?:value=")(.*?)"/
/<fin-streamer(?:.*?)(?:regularMarketChangePercent)(?:.*?)(?:value=")(.*?)"/
var input = '<fin-streamer  data-symbol="LU2197908721.SG" data-test="qsp-price" data-field="regularMarketPrice" data-trend="none" data-pricehint="2" value="115.24" active="">115.24</fin-streamer>'
var input2 = '<fin-streamer  data-symbol="LU2197908721.SG" data-field="regularMarketChangePercent" data-trend="txt" data-pricehint="2" data-template="({fmt})" value="0.0066386657" active=""><span >( 0.66%)</span></fin-streamer>'

var output = input.match(/<fin-streamer(?:.*?)(?:regularMarketPrice)(?:.*?)(?:value=")(.*?)"/)

var output2 = input2.match(/<fin-streamer(?:.*?)(?:regularMarketChangePercent)(?:.*?)(?:value=")(.*?)"/)


console.log(output[1]) // 115.24
console.log(output2[1]) // 0.0066386657

CodePudding user response:

You should use XML service build in function. See full documentation of XML Service.

It allow you to be more concise and easier to understand rather than regex.

See bellow using XmlService :

const xmlTemplate = `<fin-streamer  data-symbol="LU2197908721.SG" data-field="regularMarketChangePercent" data-trend="txt" data-pricehint="2" data-template="({fmt})" value="0.0066386657" active=""><span >( 0.66%)</span></fin-streamer>`
function myFunction() {
  let document = XmlService.parse(xmlTemplate);
  console.log(document.getRootElement().getValue()) //( 0.66%)
  console.log(document.getRootElement().getAttribute("value").getValue()) //0.0066386657
}

If regex is mandatory, you can use this (?<=value\=\")\d .\d It's taken from another post Getting the text that follows after the regex match

  • Related