2

I am trying to parse this page!

string str2 ("<span class=\"time_rtq_ticker\"><span id=\"yfs_l10_usdsgd=x\">");
  size_t found;

  // different member versions of find in the same order as above:
  found=sdata.find(str2);
  if (found!=string::npos)
    cout << "first 'needle' found at: " << int(found) << endl;

How can i obtain the Currency rate after i get the position of "needle" which is the pattern, i want it stop parsing after

Sorry i am doing this for a small project of mine, not commercial, just some mini work

Baoky chen
  • 183
  • 7
  • 15

3 Answers3

3

Doesn't yahoo offer a Web Service for this? Which would be simpler. Check this:

http://developer.yahoo.com/finance/company.html

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • that 1 for company finance, i want is currency eurusd etc. – Baoky chen Jul 23 '12 at 16:28
  • @Baokychen if you search on the site for "webservice" you will find plenty of them. you just have to find the right one. Using webservice will rid you from rechanging the code everytime the page source code is changed – Adel Boutros Jul 23 '12 at 16:36
2

std::string.substr(pos,npos) will give you the std::string from pos (found in your case) with length npos. To find out the length you might have to look for the string "<" (beginning of the next html tag) and subtract.

steffen
  • 8,572
  • 11
  • 52
  • 90
1

I think you should either use the API provided by Yahoo Finance as said by @Adel Boutros, or you should at least use a full-on HTML parser class if you want to parse things like this yourself.

There's many variations, but essentially, they'll read the tags and provide you the data content - you just need to add some handlers to catch the tags your interested in. Every parser works a little differently and has different advantages interms of speed or simplicity, but they're pretty straight forward and will be more... stable? than what you're doing.

Here's an interesting SO on choosing a C++ HTML Parser (but it looks shaky). Personally, I'd just make external calls to python to parse it, or make a little java EXE you can call to parse the downloaded webpage into a more usable format for you (I like python better, but more people know Java and it'll work too).

https://stackoverflow.com/questions/489522/library-recommendation-c-html-parser

Community
  • 1
  • 1
John Humphreys
  • 37,047
  • 37
  • 155
  • 255