Baoky chen
Baoky chen

Reputation: 183

C++ how to get substring after get position of its index

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

Upvotes: 2

Views: 3685

Answers (3)

steffen
steffen

Reputation: 8958

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.

Upvotes: 2

John Humphreys
John Humphreys

Reputation: 39294

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

Upvotes: 1

Adel Boutros
Adel Boutros

Reputation: 10285

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

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

Upvotes: 3

Related Questions