Parsing data google sheets (javascript) using Xpath or RegExp - javascript

I am trying to parse data from the following website (https://www.fundsquare.net/security/information?idInstr=275136)
I want to display the price of the fund in Google sheets. However, when using the 'importxml' function I get an error that the "imported content is empty". Anybody know what I can do to fix it?
Ways I tried the function:
=IMPORTXML("https://www.fundsquare.net/security/summary?idInstr=275136" ,"//*[#class ='surligneorange']" )
=IMPORTXML("https://www.fundsquare.net/security/information?idInstr=275136" , "//*[#id='content']/table[2]/tbody/tr/td[3]/span[1]")
=IMPORTXML("https://www.fundsquare.net/security/information?idInstr=275136" , "//*[#id='content']//span[1]")
I keep on getting the same error. When looking for this error I get the difference between static and dynamic data. This data changes so I guess its dynamic but i'm not sure how that would impact the formula.
I have been trying some things with script editor but no success. Also trying something with RegExp but couldn't get any further than the examples. My knowledge of scraping is limited so any tips and tricks when trying to parse data is greatly appreciated!
Any help would be greatly appreciated!
Edit:
Within script editor I tried the following code:
function importdata() {
var found, html, content = '';
var response = UrlFetchApp.fetch("https://www.fundsquare.net/security/information?idInstr=275136");
if (response) {
html = response.getContentText();
if (html) content = html.match(/<span class="surligneorange">(.*)<\/span>/)[0];
}
Logger.log(content);
}
This gives me the following log output:
[20-06-05 07:44:58:529 PDT] <span class="surligneorange">31.15 EUR</span> <span style="color:#DD0000;text-align:left;padding:4px 0;"> -0.67 % <img src="/images/share/variationNegative.gif" style="vertical-align:middle;"/></span></td></tr></table><div id="onglet">DocumentsTaxesDividendsHist. PricesPriceOrder Ref. Data<a class="selected" href="/security/information?idInstr=275136">Security Information</a>Overview<br class="clear_r"/></div><div id="blocresume"><table class="portlet100pct" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" class="portletleft50pct"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" class="portletBordGris"><div style="position: relative; left: 1px;" class="bloctitle"><img src="/Fundsquare/images/share/x.gif" border="0" height="1" width="1" /></div><DIV class="bloctitle" style="position: relative; top: -21px; right: 1px;"><span style="top: 3px;" >General information</span>
The value 31.15 is what I want to scrape. How can I get this value in my spreadsheet?
Edit 06/06 10:14:
further questions
Could you please help me understand what you changed. What exactly is the difference between what I tried to match and what you matched.
mine:
if (html) content = html.match(/<span class="surligneorange">(.*)<\/span>/)[0];
yours:
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*?<\/span>/)[1];
and:
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*<\/span>/)[1];
What is the difference between my [0] and your [1]. is it that you only request the first value?
What is the difference between my .* and your ([\d.]*).* or [\d.]*).*????
My knowledge from javascript is not so good so I am unsure what it does. Thanks for the help!

How about this answer?
Unfortunately, when I saw the HTML and your formulas, I thought that the value might not be directly retrieved by IMPORTXML. So I think that your approach to use Google Apps Script can be used for retrieving the value you expect. But I think that your script has the modification points a little for achieving it. In this answer, I would like to propose the modification points of your Google Apps Script.
In your case, I think that the method of match and the regex are required to be modified.
Modified script
When your script is modified, please modify as follows.
From:
if (html) content = html.match(/<span class="surligneorange">(.*)<\/span>/)[0];
To:
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*?<\/span>/)[1];
In my test, the modified script returns 31.15.
I think that <span class="surligneorange">([\d.]*).*<\/span> can be also used.
Script for putting to a cell in Google Spreadsheet
As the modified script for putting to a cell in Google Spreadsheet, from your situation, I thought that you might want to use this as the custom function. If it's so, how about the following modified script?
When your script is used as the custom function, please rename the function name from importdata to others. Because importdata has already been used as the built-in function.
function sample() {
var found, html, content = '';
var response = UrlFetchApp.fetch("https://www.fundsquare.net/security/information?idInstr=275136");
if (response) {
html = response.getContentText();
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*<\/span>/)[1];
}
return content;
}
In this case, when =sample() is put to a cell, the value of 31.15 is put to the cell. If you want to put the value as the number, please modify return content to return Number(content).
Reference:
match()
Added:
For your additional question, I added the answers as follows.
In your script, html.match(/<span class="surligneorange">(.*)<\/span>/)[0]; returns the full match. When you want to retrieve the group, in this case, it's html.match(/<span class="surligneorange">(.*)<\/span>/)[1];. But, in this regex, the retrieved value is 31.15 EUR</span>.....General information</span>. I thought that the reason of your issue is this.
In order to retrieve the value of 31.15 EUR, .* is modified to .*?. By this, the matched value is from <span class="surligneorange">31.15 EUR</span>.....General information</span> to <span class="surligneorange">31.15 EUR</span>.
But in your goal, you want to retrieve only 31.15. So I used ([\d.]*).*. By this, 31.15 of <span class="surligneorange">31.15 EUR</span>.....General information</span> is retrieved. When ([\d.]*).*? is used, 31.15 of <span class="surligneorange">31.15 EUR</span> is retrieved.
So, in the case of <span class="surligneorange">([\d.]*).*?<\/span>, <span class="surligneorange">31.15 EUR</span> is matched. And ([\d.]*) is 31.15.
In the case of <span class="surligneorange">([\d.]*).*<\/span>, <span class="surligneorange">31.15 EUR</span>.....General information</span> is matched. And ([\d.]*) is 31.15.
Reference:
Regular expressions

Related

Need to display javascript value in anchor tag of jsp

I need to pass the userid variable through anchor tag .. Here I attached code for that.
Code :
<td colspan="2" align="right"><div align="center">User Id</div></td>
<td><input class="textbox" type="text" id="userid" ><a href="access.jsp?userid='+userid'"
"></a></td></tr>
Javascript :
var userid ="MK";// this value is getting fetched correctly using document.getElementByName
Just I need to display in anchor tag.How can I do this?
Please, try to be more clear with your Question.
You should show all related parts of your code so people can figure out what exactly you need.
Also Stackoverflow provides some markup so you can insert blocks of code - very useful, I recommend looking it up :)
Assuming you already have the anchor <a> tag
HTML
JS
// your code (fetching data) ...
document.getElementById('anchor').setAttribute('href', 'google.com?userid=" + userid)

How to use an array in a text-to-speech script

Hello stackoverflow Community,
I am working on a Survey which uses Text-To-Speech components. Usually they look like this:
<img src="picture.jpg" border="1" input onclick="responsiveVoice.speak('spoken_Text_here', 'Deutsch Female');" class="button1"> Text_behind_Button?
Now, I have a Database with all the Elements which will be "spoken" (or: to be used in the TTS script) and insert them in my survey software with
$key = 'A1';
$tts = dbGet($key);
So far so good. Now I would like to take the first element of the array $tts und put it in the TTS script.
<img src="picture.jpg" border="1" input onclick="responsiveVoice.speak('<?php $tts[0] ?>', 'Deutsch Female');" class="button1"> Text_behind_Button?
As a result I would like, that when you click on the button, the script takes the string from the Database and inserts it the TTS code. This way above obviously doesn't work. So I guess that I have to change the array in a string which can be read. Unfortunatly, I dont have the knoledge how to do that.
Ideas, hints and criticism (more or less) are welcome!
Best wishes

making links clickable while pushing the data into templates

I am taking inputs from user, then adding links for mentioned users and then passing the same in the template
Input: hello #ds
String after adding links -
"#<a class="tweet-url username" href="/user/ds" data-screen-name="ds" rel="nofollow">ds</a>"
Passing the above string in .Msg (using golang template) :
<div class="panel-body" >
<p > {{.Msg}} </p>
</div>
Expected outcome is: Hello #ds (with clickable link on #ds)
However getting everything in text format (same as input).
#<a class="tweet-url username" href="/user/ds" data-screen-name="ds" rel="nofollow">ds</a>
What am I missing?
Got a better solution. First of all I am doing htmlEscape on the input then store it in db, then while presenting adding links followed by using document.write(string) function. With this I dont have to change the template and I dont have to worry about XSS attach. Also I am also avoiding XSS scripts in my database. –
Try wrapping your string (Msg) in template.HTML to disable the escaping that html/template does.
Example from the docs:
The template
Hello, {{.}}!
can be invoked with
tmpl.Execute(out, template.HTML(`<b>World</b>`))
to produce
Hello, <b>World</b>!
instead of the
Hello, <b>World<b>!
that would have been produced if {{.}}
was a regular string.
Note that you should do this with great care... make sure that you trust the string you're wrapping in template.HTML. This is an easy way to open yourself up to XSS attacks.

Micro data from json

I was wondering how to get through the testing tool of Google when it comes to rich snippets or micro data and using json data.
The thing is that I don't have the option to use PHP and only javascript. So when I want to grab review data from let's say Trusted Shops for a shop I never get passed the testing tool because the data is always empty when Google crawls the page.
So what I mean is this:
There's a direct link to the json file with the review data
http://api.trustedshops.com/rest/public/v2/shops/X17BD396442BCEE0808C79156D0E95F97/quality/reviews.json
What I tried is this
<script type="text/javascript">
window.onload = function(){
var url = 'http://api.trustedshops.com/rest/public/v2/shops/X17BD396442BCEE0808C79156D0E95F97/quality/reviews.json';
$.getJSON(url, function(data){
var stuff = data.response.data;
var review = stuff.shop.qualityIndicators
$('#value').html(review.reviewIndicator.overallMark);
$('#votes').html(review.reviewIndicator.activeReviewCount);
});
}
</script>
HTML output
<span xmlns:v="http://rdf.data-vocabulary.org/#" typeof="v:Review-aggregate">
<span rel="v:rating">
<span property="v:value" id="value">4.83</span> /
<span property="v:best" id="best">5.00</span>
</span> of
<span property="v:votes" id="votes">58</span> reviews.
</span>
Now with the Testing tool from Google I always get 3 errors saying "can't leave blank...".
Is such thing even possible??
So I have no clue how to use this tool, but none of their examples have javascript in them. I don't think this tool will execute any javascript in the left hand box before it tries to validate the data. There are examples with <script type="application/ld+json"> tags, but they are just JSON data, and not executable code (note the type).
To verify this I simply entered this snippet:
<script type="text/javascript">
console.log('it this on?');
</script>
When I click validate, nothing shows up in my browser console.
Whatever you are trying to make this tool do, I think is outside the scope of how it's intended to be used. It appears that this tools can validate the result of your code, but it cannot also run your code.

HTML/Javascript search function with AJAX

I've created a webpage where I want users to be able to search for a word/term stored in a CSV file, and if that term is found the full line for that line entry will be returned and displayed to the user (ideally in table format, otherwise a textarea will do).
But I need to do this using AJAX, and I also cant use PHP (unfortunately, otherwise I wouldn't be asking this question).
So far I have a table for the form/input/button, and I've also got the code to read the file, but I'm a bit stuck with bringing both together. I know this should be an easy thing to do, but I've spend a lot of time going through tutorials and online questions but havent been able to find anything similar.
If anyone knows of any tutorials that covers this, or can help out with the code below it would be appreciated.
<table>
<tr><td>Enter Search Term:
<input type="text" name="searchword" />
<input type="button" name="searchbutton" value="Search" onclick="contentDisp();">
</td></tr>
<tr><td><textarea id="contentArea" rows="40" cols="60"></textarea></td></tr>
</table> //currently using text area but ideally this would be displayed in a table
<script type="text/javascript">
function contentDisp()
{
$.ajax({
url : "file.csv",
success : function (data) {
$("#contentArea").html(data); // I THINK SOMETHING NEEDS TO GO IN HERE, WHICH WILL GRAB THE SEARCH TERM ABOVE AND THEN ONLY DISPLAY FILE CONTENTS USING THAT TERM, POSSIBLY 'CONTAIN' */
}
});
}
</script>
It is possible to do this strictly via JavaScript by using some strpos and indexOf functions (indexOf is the starting point, while the other will look for the string delimiter(s) ).
it is also possible to do the task with php if you feel comfortable with it, if you're restricted by domain-origin restriction, take a look at JSONP, which stands for JSON with Padding - which basically means that you'll need to wrap the result in a JavaScript function.
good luck.
User Regular Expressions to find your string and to parse the found line in the CSV data.
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
HTML
<input type="text" id="text" />
<input type="submit" id="btnsubmit" />
Script
$(function(){
$('#btnsubmit').on('click', function(){
var csv = $.ajax('text.csv');
csv.done(function(data){
var str = data.split(',');
var value = $('#text').val();
$.each(str, function(index, item){
if(item.match(value)){
console.log(item) //Output
};
})
})
})
});
CSV
Presidency ,President ,Wikipedia Entry,Took office ,Left office ,Party ,Portrait,Thumbnail,Home State
Why would you use strpos and indexOf when javascript already has built-in functions for matching strings?
http://jsfiddle.net/AWZg8/

Categories

Resources