#include <string>
#include <fstream>
#include "curl.h"
#pragma comment(lib, "libcurl.lib")
using namespace std;
int writer(char* data, size_t size, size_t nmemb, string* writerData)
{
size_t sizes =size*nmemb;
if(writerData == NULL) return 0;
writerData->append(data, sizes);
return sizes;
}
int main()
{
CURL* curl = NULL;
string buffer;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://www.weather.com/weather/5-day/CHXX0046");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if(buffer.size())
{
ofstream fout;
fout.open("temp.html");
fout << buffer;
fout << flush;
fout.close();
}
return 0;
}
取到的html文件中,包含有需要的气象信息。只不过默认是华氏温度的。
我也试过加cookie,来自动获取摄氏的温度,不过失败了。
原因可能是,控制切换显示华氏、摄氏的功能是javascript+css来实现的,libcurl对非html都统一无视了。
不过取到华氏也就够了,摄氏度(℃)=(华氏度(℉)-32)÷1.8很简单的。 libcurl是mit风格的开源库, 可以静态编译后嵌入程序,而不需多带一个dll文件。
静态编译也很简单,官方提供的下载都是带dll的,而且还是老版本。
下载新的代码包后,解压,启动msvc的命令行,来到源代码的目录
cd winbuild
nmake -f makefile.vc mode=staitc ENABLE_IDN=no
编译好后,就可以在源代码的目录下看到builds目录,编译好的结果就在里面了。
使用时,还要加编译开关/DCURL_STATICLIB,或者干脆手动在curl.h里加上一行#define CURL_STATICLIB 搞好了,我泪流满面啊
页:
1
[2]