Get a numeric value from a website
Hey Guys, I am trying to get a value from a website.
<span id="insert-graph">3142</span>
The value being 3142.
I have been successful in accessing the website. I have also been successful in grabbing a value using "string search". The problem i am having at is that the value is static. That indicates to me that the value is not being updated. If anyone has any ideas please let me know. Thank you so much!
void Start () {
//curDemand = maxDemand;
InvokeRepeating ("demandCalc", 1f, 1f);
string url = "https://www.energex.com.au/the-network/understanding-the-network/peak-demand";
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
string input = "<span id=\"insert-graph\">3166</span>";
string search = "<span id=\"insert-graph\">";
int p = input.IndexOf (search);
if (p >= 0) {
// move forward to the value
int start = p + search.Length;
// now find the end by searching for the next closing tag starting at the start position,
// limiting the forward search to the max value length
int end = input.IndexOf ("</span>", start);
if (end >= 0) {
// pull out the substring
string v = input.Substring (start, end - start);
// finally parse into a float
float value = float.Parse (v);
Debug.Log ("Value = " + value);
curDemand = value;
} else {
//Debug.Log("Bad html - closing tag not found");
}
} else {
Debug.Log ("donations span not found");
}
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null) {
Debug.Log ("WWW Ok!: " + www.text);
//site.text = "site" + www.text;
} else {
Debug.Log ("WWW Error: " + www.error);
}
}
What i can see from this is that you are not using www.text ins$$anonymous$$d you are using a fixed input variable.
int p = input.IndexOf (search);
should be
int p = www.text.IndexOf (search);
Hey Yash, Thanks for your comment. I have changed the lines you have suggested. I am however getting this error now: UnityException: WWW is not ready downloading yet.
void Start () {
//curDemand = maxDemand;
InvokeRepeating ("demandCalc", 1f, 1f);
string url = "https://www.energex.com.au/the-network/understanding-the-network/peak-demand";
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null) {
Debug.Log ("WWW Ok!: " + www.text);
string input = www.text;
string search = "<span id=\"insert-graph\">";
int p = input.IndexOf (search);
if (p >= 0) {
// move forward to the value
int start = p + search.Length;
// now find the end by searching for the next closing tag starting at the start position,
// limiting the forward search to the max value length
int end = input.IndexOf ("</span>", start);
if (end >= 0) {
// pull out the substring
string v = input.Substring (start, end - start);
// finally parse into a float
float value = float.Parse (v);
Debug.Log ("Value = " + value);
curDemand = value;
} else {
//Debug.Log("Bad html - closing tag not found");
}
} else {
Debug.Log ("donations span not found");
}
} else {
Debug.Log ("WWW Error: " + www.error);
}
}
Answer by NG_Phteven · Dec 23, 2015 at 03:08 PM
Alright, I'm pretty sure I figured out the reason why we can't access that number. The script works fine. The problem is with the webpage itself or rather with the way the number is produced. Since the number isn't static and gets updated, it's not actually in the html source properties of the webpage. This means that the number is received from a server or similar. Since it's not in the html source code, we can't access it because the html source is what we access using the script.
The easiest way is to contact the admin of the page and ask for a way to let you access that number. I assume it would be another html site with just the number in it which actually gets called by the main site.
Your answer
![](https://koobas.hobune.stream/wayback/20220612053506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UnityWebRequest, C# request.accept equivalent? 0 Answers
How to get variable by name(string typed in inspector) in array with index from another script? 0 Answers
Get keyframe values of animations via script 1 Answer
How to GET and parse data from website? 0 Answers
Getting Values In Unity From Webpage 0 Answers