微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何捕获C ++ wget URL命令的重定向URL

如何解决如何捕获C ++ wget URL命令的重定向URL

我正在编写一些c ++代码,该代码根据玩家人数将URL爬到前10个Steam游戏中,然后计划使用这些URL从游戏中获取图像。总体而言,我使用的系统用于获取那些找到的URL和查找图像链接,但游戏年龄限制或游戏首页与标准Steam游戏布局不匹配时除外。

我有前10个游戏URL的向量,但是当向量中有类似GTA V的游戏时,该链接重定向到新链接。我注意到新的重定向URL包含字符串“ agecheck”,因此我可以轻松编写一个小的if块,跳过所有使用该字符串重定向到URL的游戏,但是我不确定如何捕获重定向URL。我正在从此URL https://store.steampowered.com/stats/Steam-Game-and-Player-Statistics

提取链接

我的问题的一个示例是我的代码为GTA V找到了该URL https://store.steampowered.com/app/271590/Grand_Theft_Auto_V/

但是该链接重定向到URL

https://store.steampowered.com/agecheck/app/271590/

给出一个包含URL的字符串,如何找到链接重定向到的URL?或者如何避免重定向

下面是我到目前为止的代码,我想捕获collect_images_url函数中的重定向URL,以便可以在该函数中编写一个if循环。 我完全知道此代码完全没有记录,而且看起来也不是很好,但是现在我首先要寻找功能

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>

int system(const char *command);
std::vector<std::string> collect_top_ten();
std::string collect_image_url(std::string url);

int main()
{
  std::vector<std::string> links = collect_top_ten();
  std::string temp;

  for (int i = 0; i < links.size(); i++)
  {
    temp = collect_image_url(links.at(i));
    std::cout << "\n\nFOUND URL: " << temp << std::endl;
    links.at(i) = temp;
  }

  for (int i = 0; i < links.size(); i++)
  {
    std::cout << links.at(i) << std::endl;
  }

  return 0;
}

std::string collect_image_url(std::string url)
{
  std::string command = "wget " + url;
  system((const char*)command.c_str());
  std::ifstream ifs("index.html");
  std::string content( (std::istreambuf_iterator<char>(ifs)),(std::istreambuf_iterator<char>()));

  std::size_t pos = content.find("strip_screenshot");
  content = content.substr(pos);

  pos = content.find("http");
  content = content.substr(pos);
  
  std::size_t pos2 = content.find(">");
  content = content.substr(0,pos2);

  command = "rm index.html";
  system((const char*)command.c_str());

  return content;
}

std::vector<std::string> collect_top_ten()
{
  std::string url = "https://store.steampowered.com/stats/Steam-Game-and-Player-Statistics";
  std::string command = "wget " + url;
  system((const char*)command.c_str());
  std::ifstream ifs("Steam-Game-and-Player-Statistics");
  std::string content( (std::istreambuf_iterator<char>(ifs)),(std::istreambuf_iterator<char>()));

  std::size_t pos = content.find("detailStats");
  content = content.substr(pos);
  
  std::vector<std::string> urls;

  std::string temp;

  for(int i = 0; i < 10; i++)
  {
    pos = content.find("href");
    content = content.substr(pos);
    std::size_t pos2 = content.find(">");

    temp = content.substr(0,pos2);
    temp = temp.erase(0,6);
    temp = temp.substr(0,temp.size()-1);

    urls.push_back(temp);

    content = content.substr(pos2);
  }
 
  command = "rm Steam-Game-and-Player-Statistics";
  system((const char*)command.c_str());

  return urls;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。