前提条件系统安装有curl
#include <iostream> // 包含输入输出流库
#include <string> // 包含输入输出流库
using std::string;
#include <cstdio> // 引入库以使用popen和pclose函数
int main() { // 主函数,程序的入口
std::cout << "Hello, World!" << std::endl; // 输出 Hello, World!
readBaidu();
string s;
while("1"!=s){
std::cin >> s;
std::cout << s << std::endl;
}
return 0; // 表示程序正常终止
}
int readBaidu(){
FILE* pipe = popen("curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' -H 'Referer: https://google.com?q=example' -i -k https://www.baidu.com/", "r"); // 打开管道,执行命令并读取输出
if (!pipe) {
return 1; // 如果打开管道失败,则退出
}
char buffer[128]; // 读取输出时使用的缓冲区
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL) {
printf("%s", buffer); // 打印命令的输出
}
}
pclose(pipe); // 关闭管道
return 0;
}