Created
September 27, 2013 06:35
-
-
Save vs4vijay/6724868 to your computer and use it in GitHub Desktop.
Extract Data from XPath via Google Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDataFromXpath(path, url) { | |
var data = UrlFetchApp.fetch(url); | |
var text = data.getContentText(); | |
var xmlDoc = Xml.parse(text, true); | |
// Replacing tbody tag because app script doesnt understand. | |
path = path.replace("/html/","").replace("/tbody","","g"); | |
var tags = path.split("/"); | |
Logger.log("tags : " + tags); | |
// getting the DOM of HTML | |
var element = xmlDoc.getElement(); | |
for(var i in tags) { | |
var tag = tags[i]; | |
Logger.log("Tag : " + tag); | |
var index = tag.indexOf("["); | |
if(index != -1) { | |
var val = parseInt(tag[index+1]); | |
tag = tag.substring(0,index); | |
element = element.getElements(tag)[val-1]; | |
} else { | |
element = element.getElement(tag); | |
} | |
//Logger.log(element.toXmlString()); | |
} | |
return element.getText() ;//+ ' [ ' + element.getAttribute("href").getValue() + ' ] '; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yea this code snippet does not work any more. Which is not surprising giving that it was published 9 years ago