C ++,我如何在html代码中获取特定字符串

时间:2012-07-23 15:42:45

标签: c++ wget

我正在尝试解析this XML Yahoo feed

我如何将每条记录都放入C ++中的数组中 比如创建一个结构

然后得到那些变量 并记录结构中的每个元素。

首先,我如何获得价值

由于

2 个答案:

答案 0 :(得分:5)

您可能想要查看给定页面是否以JSON格式提供输出。然后你可以简单地请求值而不是搞乱HTML。雅虎!财务网站甚至可以提供可用于轻松请求价值的API。

答案 1 :(得分:0)

如果你想搞乱HTML代码:

#include <iostream>
#include <fstream>
int main() {
  std::ifstream ifile("in.html");
  std::string line;
  std::string ndl("<span id=\"yfs_l10_sgdmyr=x\">");
  while(ifile.good()){ 
    getline(ifile, line);
    if (line.size()) {
      size_t spos, epos;
      if ((spos = line.find(ndl)) != std::string::npos) {
        spos += ndl.size();
        if ((epos = line.find(std::string("</span>"), spos)) != std::string::npos) {
          std::cout << line.substr(spos, epos-spos) << std::endl;
        }   
      }   
    }   
  }   
  return 0;
}