Home > Blockchain >  How can I get an element by Id or class in a Google Script?
How can I get an element by Id or class in a Google Script?

Time:12-20

For a project I am working on I want to extract all EAN numbers from a list of different URL's in Google Sheet.

Now I am using the URL Fetch app method to get the HTML of the link but when I want to select the element I want to use in my script, I get the following error:

TypeError: html.getElementById is not a function

My code

function scrapeWebsite() {
  var response = UrlFetchApp.fetch('https://www.bol.com/nl/nl/p/azul-tuin-van-de-koningin-bordspel/9300000094065315/?promo=main_860_product_4&bltgh=kj5mIYO78dnIY1vUCvxPbg.18_19.24.ProductTitle');
  var html = response.getContentText();
  var element = html.getElementById('test')
}

CodePudding user response:

I think you need to await that fetch. Then take the response and .innerHTML it into a placeholder element like below:

async function scrapeWebsite() {
let response = await UrlFetchApp.fetch('https://www.bol.com/nl/nl/p/azul-tuin-van-de-koningin-bordspel/9300000094065315/?promo=main_860_product_4&bltgh=kj5mIYO78dnIY1vUCvxPbg.18_19.24.ProductTitle');
let html = response.text();
let element = html.getElementById('test')
let el = document.createElement( 'html' );
el.innerHTML = response;
}

CodePudding user response:

To get an element by its ID or class in a Google Script, you can use the getElementById or getElementsByClassName method of the Document object.

Here is an example of how to use these methods:

// Get the element with the ID "my-element"
var elementById = Document.getElementById("my-element");

// Get all elements with the class "my-class"
var elementsByClass = Document.getElementsByClassName("my-class");

The getElementById method returns a single element, while the getElementsByClassName method returns a collection of elements.

You can also use the querySelector and querySelectorAll methods to select elements using CSS selectors. For example:

// Get the first element matching the CSS selector ".my-class .nested-class"
var element = Document.querySelector(".my-class .nested-class");

// Get all elements matching the CSS selector ".my-class .nested-class"
var elements = Document.querySelectorAll(".my-class .nested-class");

Keep in mind that these methods will only work if you are running your script in the context of a document, such as in a Google Sheets or Google Docs script. If you are not running your script in the context of a document, you will need to use a different approach to select elements.

CodePudding user response:

To get an element by its ID or class in a Google Script, you can use the JavaScript document object, which is available in Google Scripts. However, you need to create a Document object from the HTML content that you fetch using the UrlFetchApp method.

Here's an example of how you can do this:

function scrapeWebsite() {
  var response = UrlFetchApp.fetch('https://www.bol.com/nl/nl/p/azul-tuin-van-de-koningin-bordspel/9300000094065315/?promo=main_860_product_4&bltgh=kj5mIYO78dnIY1vUCvxPbg.18_19.24.ProductTitle');
  var html = response.getContentText();
  var doc = XmlService.parse(html);
  var element = doc.getElementById('test');
}

The XmlService object is part of the Google Apps Script library and allows you to parse and manipulate XML and HTML.

Once you have the Document object, you can use the getElementById method to get an element by its ID, or the getElementsByClassName method to get elements by their class name.

For example:

var element = doc.getElementById('test');
var elements = doc.getElementsByClassName('my-class');
  • Related