c++ - 如何捕获 C++ wget URL 命令的重定向 URL
问题描述
我正在编写一些 c++ 代码,根据玩家数量抓取前 10 名 Steam 游戏的 URL,然后计划使用这些 URL 从游戏中获取图像。总的来说,除了游戏有年龄限制或游戏主页与标准 Steam 游戏布局不匹配时,我用来获取这些找到的 URL 并查找图像链接的系统都可以正常工作。
我有一个前 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;
}
解决方案
推荐阅读
- javascript - 用javascript中的正则表达式替换文本
- c# - asp.net core web api无法从客户端发送对象
- javascript - 如何解决通过id查找商品的问题?
- angular - 如何在 Angular 6+ 上基于 --project 导入脚本?
- css - 如何修改此 Bootstrap 列堆叠行为
- yii2 - 更新 Yii2 项目到 php 7.2
- javascript - Jquery中的变量未在javascript中定义
- java - 使用 GenericDAO 插入第二个数据源时出错
- python - Pandas 根据列的每个唯一元素添加行
- php - CodeIgniter Eloquent 模型关系类模型未找到