- 注册时间
- 2004-11-1
- 最后登录
- 2018-4-24
版主
  
- 积分
- 548
|
发表于 2012-9-30 09:01:17
|
显示全部楼层
与ssl、zlib半毛钱的关系都没有的说- #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 很简单的。 |
|