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

C++ , how do i get a particular string in a html code

本文关键字:字符串 代码 html C++      更新时间:2023-10-16

我正在尝试解析这个XMLYahoo提要。

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

然后得到这些变量并记录结构内部的每个元素。

首先,我如何从中获得价值

感谢

您可能想看看给定页面是否提供JSON格式的输出。然后,您可以简单地请求该值,而不是乱用HTML。雅虎!金融网站甚至可以提供API,您可以使用它轻松地请求价值。

如果你想处理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;
}