Advertisement
Guest User

hl_price - extracts fund price from hl website

a guest
Dec 1st, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. // hl_price function to extract share prices for OEIC / UT from Hargreaves Lansdown webpage
  2. //
  3. // Created because google IMPORTXML function wasn't loading
  4. //
  5. // See the following link for a problem description
  6. // https://support.google.com/docs/forum/AAAABuH1jm0YMhLzXEfwkI/?hl=en
  7. //
  8. // Code obtained from
  9. // https://stackoverflow.com/questions/39014766/to-exceed-the-importxml-limit-on-google-spreadsheet/48483734#48483734
  10. //
  11. // Also see following link for a slightly different approach using xml instead of regex to get the price
  12. // https://stackoverflow.com/questions/41253339/trouble-with-importxml-on-google-sheets/41275946#41275946
  13. //
  14. // todo perhaps need to publish the script so that it can be called from other google sheets?
  15.  
  16. function importRegex(url, regexInput) {
  17. var output = '';
  18. var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
  19. if (fetchedUrl) {
  20. var html = fetchedUrl.getContentText();
  21. if (html.length && regexInput.length) {
  22. output = html.match(new RegExp(regexInput, 'i'))[1];
  23. }
  24. }
  25. // Grace period to not overload
  26. // Utilities.sleep(1000);
  27. return output;
  28. }
  29.  
  30. function hl_price(url) {
  31.  
  32. // regex regular expression to extract the bid price
  33. // HL price looks like this
  34. // <span class="price-label">Sell:</span><span class="bid price-divide" >30,056.71p</span>
  35. var bid_price_regex = '<span class="price-label">Sell:</span><span class="bid price-divide" >([0-9,]*\.[0-9]+[0-9]+)p</span>';
  36.  
  37. // Grace period to not overload
  38. Utilities.sleep(100);
  39.  
  40. return importRegex(url, bid_price_regex);
  41. }
  42.  
  43. // test function - run with Run->Run function->run_test, then look at View->Logs to see output
  44. function run_test() {
  45.  
  46. url = "https://www.hl.co.uk/funds/fund-discounts,-prices--and--factsheets/search-results/v/vanguard-global-small-cap-index-accumulation"
  47.  
  48. Logger.log(url);
  49.  
  50. price = hl_price(url);
  51.  
  52. Logger.log(price);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement