get value from website
Hello! I really need some help. I need to get a value from a website. the website is http://www.pegboardnerds.com/ and I need to get the value from
<span id="donations">8470.71</span>
which is shown as: 8470.71
any way or method possible is fine by me, (even converting it all to a string and then doing some un-optimized witchcraft) but a really important part of my game depends on that value. I'm also not the direct owner of the website so I can't edit anything (however I am allowed to use it, so don't worry about any legal issues, etc)
If possible, I'd really appreciate any help to be in C#
thanks in advance for any help, everyone!
you can use
string v = input.Substring (start, end - start);
float value = float.Parse (v);
Debug.Log ("Value = " + value);
to parse a value
Answer by Dave-Carlile · Oct 09, 2015 at 03:56 AM
Use the WWW class to grab the web page, then search for the string you're interested in and parse out the value.
There's a number of ways you could parse the html. Your best bet would be to use something built-in to .NET to convert it to an object model, or some library to do that. Or you could parse it manually...
Here is some really crappy code to do it... some would argue that you should use regular expressions or something.
string input = "blah blah blah<blah><span id=\"donations\">8470.71</span></blah>";
string search = "<span id=\"donations\">";
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);
}
else
{
Debug.Log("Bad html - closing tag not found");
}
}
else
{
Debug.Log("donations span not found");
}
That sounds perfect, and I already started with that, but I don't know how to parse out the value. could you help me with that?
Oh, is it updating the value using javascript/ajax (or whatever they're calling that nowadays)?
You'd have to look through the page source, or chrome dev tools or something and find the javascript that's executing and figure out what it's doing. It's possibly calling back to the server to grab the value and update it periodically.
Alright. I'll do my best but there's about 6 very long scripts. Anyway, thanks for all the help! ps. If I find where it's getting the value from, how can I grab that?
It may be more html, or it might be json. $$anonymous$$ore parsing :).
Hey xXJuanXx,
Did you figure out a solution? I am having the exact same problem as you!
Hey! Dave Carlile's script worked fine for me, but the only problem I was having was that the website I tried it on used a weird script to animate the value so that's the only reason it didn't work.
(It still doesn't work, and I actually had my idea stolen so I'm not bothering to fix it, but this script should work in every other situation)
That's very unfortunate that you didn't get it to work and then had your idea stolen :(.
I Have adopted the script and changed the variables to suit my needs. No luck however. Would you $$anonymous$$d clarifying what I need to put into the string input? Is it the whole span like this "<span id=\"insert-graph\">3166</span>";
or is there more to it? I am starting to think that I need to be more specific about where to find that span? The html I want is embedded quite deeply into the site. Hope it's not too much trouble. Thanks so much mate!
Your answer
Follow this Question
Related Questions
Get Internet Year PHP 0 Answers
[WEBGL] Post json - complicated structure 0 Answers
WWWForm - .AddField vs .headers 0 Answers
Script in Unity responding incorrect values 0 Answers
Writing Files to Android Device 0 Answers