How get content from html element in Google spreadsheet? - html

I can use following code in Google Spreadsheet to retrieve a specific <span> element on that html-document.
=IMPORTXML("https://finance.yahoo.com/quote/NBIX201218C00125000?p=NBIX201218C00125000";"//td[#data-test='PREV_CLOSE-value']/span")
But this returns the error message that the returned content is empty.
So how do I retrieve the content (in this example the text '17.00') of this HTML-element in the spreadsheet?

Use IMPORTFROMWEB addon (number of requests are limited in the free plan) to get this.
XPath used :
//td[#data-test='PREV_CLOSE-value']/span
Formula in C5 :
=IMPORTFROMWEB(C1;C2)

Maybe this is because of the consent manager on Yahoo, that is shown before you get to the actual page.
When I downloaded the page, after checked the consent, put in on my server and use your code in Google Spreadsheets it is working.

Related

How to get all TextRun elements

Using Google App Script, is there a way to get all the TextRuns (a block of text with the same format) in Google Docs?
I'm trying to create a function that batch replaces one specific text format (from black text to white text).
I've looked through the GAS reference, but can't find anything that does this. findElement() doesn't allow the ElementType TextRun. And overall TextRun doesn't seem to be a functional/manipulable element, at least in the current ref docs -- even though the Document Structure doc does mention it as a sub-element of Paragraph.
So, is there a way to get all the TextRuns? Or is there an equivalent alternative?
Text runs can be retrieved using Document.get. The runs are located at Body> Structured Element (Paragraph)>Paragraph Element>TextRun in the document json. Advanced Google services may be used to access the api from apps script.

Get the ranges of the elements in a google doc using Google Apps Script

Following on from this question - I am now unsure how I might be able to target certain paragraphs or elements within the document when using Google Apps Scripts in order to use the batch update method outlined one would need to find the range of the element that required styling.
If there were 10 empty paragraphs (with content of "\n",) in a document - how would it be possible to target the 8th paragraph and get the range values {"startIndex": xx, "endIndex": xx} relative to the document so they could be used in the batch update?
I have a feeling I am missing something very obvious here.
Using Google Docs API and the document ID you can make a documents.get request, which will return you a document resource with a body field which has a content field with all the elements in the document, where you can see the "startIndex" and "endIndex".

ImportXML with XPath in Google Spreadsheet using <span> element

I am trying to get the value of an element attribute from this site via importXML in Google Spreadsheet using XPath.
The attribute value i seek is content found in the span class="item-chart_server-price__1r2rn".
outputs is 2,427 Z
Tried using:
//*[#id='app']/div[2]/div[2]/span[2] i get #N/A;
//div[#id='app']//div[#class='item-chart_item-chart__3YMlA']//div[#class='item-chart_server__37cgg']/span[#class='item-chart_server-price__1r2rn'] i get #N/A;
//span[#class='item-chart_server-price__1r2rn'] i get #N/A;
looks like this won't be possible. all you can get is:
=IMPORTXML("https://www.romexchange.com/?q=steel","//*")
Indeed, I was not able to fetch the data using ImportXML, but I ended up being able to fetch from romexchange.com using a custom javascript function.
You can have a look at it in
https://github.com/diogovk/rom_exchange_sheets_macro/blob/master/rom_exchange_sheets_macro.js
To enable it on your sheets, go to Tools->Script editor.
After you saved a new function ROMX_latest will be available.

Google Apps Script shown incorrect values

I am using Google Apps Script to update a Google spreadsheet.
The Apps script is taking the follower count for Twitter pages.
I am using below formula for the same
I have used this URL for reference
cell.setValue('=IMPORTXML(\"'+url+'\","//a[#data-nav=\'followers\']/span[3]")');
Basically we are doing a kind of scraping here to extract the count values. The above code is looking for anchor tag with value for data-nav as followers and taking the text inside the 3rd span tag . When I inspected the page in browser , the value shown in the third span tag was 100 (for example).
But the value picked by appscript is higher than that (like 101 or 102) .
I thought it was due to caching , and we even used random numbers to avoid that , but even that didn't work.The details for that was found here
Can somebody have a look into this ?

How to get Google search result snippets (descriptions) by using importxml function?

I want use Google spreadsheet =importxml to get Google search results snippets but I don't know what XPath I should use. Could any tell me what is XPath for it?
I tried using //h3[#class='result-desc'] but it keeps telling me:
imported content is empty
=IMPORTXML("https://www.google.com/search?q=Bmw&safe=off&tbs=qdr:d", "//h3[#class='result-desc']")
What is correct XPath for it?
Remember that Google Sheets ImportXML function will often receive a different HTML than what you see in a browser. (For example it doesn't load JavaScript, etc), so you cannot rely on inspecting HTML to get your XPATHs.
In your case some examples you can try:
=IMPORTXML("https://www.google.com/search?q=Bmw&safe=off", "//*[#class='g']")
or
=IMPORTHTML("https://www.google.com/search?q=Bmw&safe=off","list",9)