如何在C ++程序中从网站获取数据 [英] How can I fetch data from a website inside a C++ program

查看:106
本文介绍了如何在C ++程序中从网站获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中编写一个帮助我管理曲棍球池的程序,我需要做的一件关键事情就是读出前一周的时间表。我希望使用NHL网站。有什么办法让程序下载给定的url的HTML文件,然后解析呢?我想一旦我有文件下载,简单文件I / O会做,但我不知道如何下载文件。

I want to write a program in C++ that helps to manage my hockey pool, and one of the key things i will need to do is read off the schedule for the week ahead. I was hoping to use the NHL website. is there any way to have the program download the HTML file for a given url, and then parse that? i suppose that once i have the file downloaded, Simple file I/O would do, but im not sure how to download the file.

推荐答案

我会使用一些提供Http抽象的库。

I would use some library providing Http abstraction.

示例:

cpp-netlib

cpp-netlib

#include <boost/network/protocol/http/client.hpp>
#include <string>
#include <iostream>
int main()
{
    boost::network::http::client client;
    boost::network::http::client::request request("http://www.example.com");
    request << boost::network::header("Connection", "close");
    boost::network::http::client::response response = client.get(request);

    std::cout << body(response);
}

我认为它不会比这更容易

I do not think it can get much easier than that

在GNU / Linux上编译:

On GNU/Linux compile with:

g++ -I. -I$BOOST_ROOT -L$BOOST_ROOT/stage/lib -lboost_system -pthread my_main.cpp

QHttp

QHttp

示例因为这可能会很长,
,因为QHttp可以只发送非阻塞请求(这意味着,你必须捕获一些信号报告请求已完成等)。
但是文档是精湛的,所以它不应该是一个问题。 :)

Example for this could get quite long, since QHttp can send only non-blocking requests (that means, that you have to catch some signals reporting that the request was finished, etc.). But the documentation is superb, so it should not be a problem. :)

这篇关于如何在C ++程序中从网站获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆