I am trying to teach myself some coding and scraping from websites. But I am having an issue with adding parameters. Without parameters I need to adjust the function which is not preferable. This is the working function without the parameters
function import1() {
var html, content = '';
var response = UrlFetchApp.fetch("https://www.fundsquare.net/security/summary?idInstr=275136");
if (response) {
html = response.getContentText();
if (html) content = html.match(/<span class="surligneorange">([\d.]*).*<\/span>/)[1];
}
return content;
}
This is how I adjusted it so it has parameters:
function importval(url, name) {
var found, html, content = '';
var response = UrlFetchApp.fetch(url);
found = "/<span class="+name+">([\d.]*).*<\/span>/"
if (response) {
html = response.getContentText();
if (html) content = html.match(found)[1];
}
return content;
}
However, It doesn't work. It gives different errors when I try to adjust it. The issue is with the URL
with some errors and with the name
with others. With this code above the error is that the url
variable has no value. I am unsure how I can make the formula with the parameters
I understand that the code found = "/<span class="+name+">([\d.]*).*<\/span>/"
wont work with every website but if I can make this work I can figure out how to adjust it so it works for the websites I want it for. Now it will only work for a span class, but this can be changed so it works for more websites (I think).
Edit 6-6 16:05 this is a question about the solution
This is the code that the match should find: (/<span class="surligneorange">([\d.]*).*<\/span>/)[1];
this is the code, you wrote ("<span class=\""+name+"\">([\\d.]*).*<\\/span>")
.
What is the reason the two \
in \""+name+"\"
are needed? When playing around with it it seems to be important for showing what part is a string and what part is a variable that should be implemented. But I am unsure how it works because "<span class=\"
includes the \
but "+name+"\"
here it seems to be in between the " "
. Why is one \
included and one on its own between " "
?
How about this modification?
RegExp
.html.match(found)
becomes null
. In this case, your script occurs an error. So I modified about this.When your script is modified, it becomes as follows.
function importval(url, name) {
var found, html, content = '';
var response = UrlFetchApp.fetch(url);
found = new RegExp("<span class=\""+name+"\">([\\d.]*).*<\\/span>"); // <--- Modified
if (response) {
html = response.getContentText();
if (html) {
content = html.match(found); // <--- Modified
if (content && content.length == 2) { // <--- Added
content = content[1];
}
}
}
return content;
}
name
is surligneorange
, the regex becomes /<span class="surligneorange">([\d.]*).*<\/span>/
.url
and name
are https://www.fundsquare.net/security/summary?idInstr=275136
and surligneorange
, respectively, 31.15
is retrieved.null
is returned.About your additional question, I would like to answer as follows.
In this case, as a simple way for understanding it, how about checking the value of found
? In the current stage, now, it has already been found that /<span class="surligneorange">([\d.]*).*<\/span>/
is the correct value.
When name
is surligneorange
,
found
of found = new RegExp("<span class="+name+">([\d.]*).*<\/span>");
is as follows.
/<span class=surligneorange>([d.]*).*<\/span>/
found
of found = new RegExp("<span class=\""+name+"\">([\\d.]*).*<\\/span>");
is as follows.
/<span class="surligneorange">([\d.]*).*<\/span>/
In this case, <\/span>
and <\\/span>
were the same result.
In this document , When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary.
can be seen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.