读书人

100分提问:在VC++中怎么获取天气预报

发布时间: 2013-01-04 10:04:12 作者: rapoo

100分提问:在VC++中如何获取天气预报温度信息并保存为txt(有源码)?
WebServer:Endpoint
Disco
WSDL

要求:用VC++获取洛阳地区(城市代码:57073)未来一天的温度信息,每一个小时获取一次,将获取的温度信息依次保存在Output.txt中保存在硬盘中。
相关参考:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName

请将以下源码改成我需要的,编译通过,给>=90分(我的权限只能发100分的帖子,O(∩_∩)O~)。急需!明晚10:00左右结贴!

我从网上下载了一位大牛的源码,现附下:

/*
天气预报
=====================================
城 市:郑州
------------------------------------
天 气:晴
------------------------------------
温 度:9℃~-2℃
------------------------------------
风 向:微风
======================================

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")


struct Weather_Info
{
char city[50];
char weather[50];
char temp[50];
char wind[50];
};


const char* URL="http://www.weather.com.cn/weather/101180101.shtml?from=cn";
const char* weather_tag="<td width=\"160\" align=\"center\" valign=\"top\" class=\"weather\">";
const char* temp_tag="<td width=\"160\" align=\"center\" valign=\"top\" class=\"weatheren\">";
const char* wind_tag="<td width=\"153\" valign=\"top\"><span class=\"big-cn\">";


bool GetWeather(Weather_Info&wi,char *html)
{
if(html==NULL)
return false;
char *temp=NULL;
if((temp=strstr(html,weather_tag))==NULL)
return false;

temp+=strlen(weather_tag);
memcpy(wi.weather,temp,strlen(temp)-strlen(strstr(temp,"<")));
if((temp=strstr(html,temp_tag))==NULL)
return false;
temp+=strlen(temp_tag);
memcpy(wi.temp,temp,strlen(temp)-strlen(strstr(temp,"<")));
if((temp=strstr(html,wind_tag))==NULL)
return false;
temp+=strlen(wind_tag);
memcpy(wi.wind,temp,strlen(temp)-strlen(strstr(temp,"<")));
return true;
}


bool initsocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return false;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return false;
}
return true;

}


struct SearchInfo//搜索结构体定义
{
char host[256];//主机名
unsigned int port;//端口号
char filename[256];//要文件名
char outfile[50];//保存文件名
};


void GetUrls(char *html);//解析html代码中的URL
bool initsocket();//初始化套接字


bool initargs(SearchInfo &outinfo,int argc,char **args);//解析输入命令行参数
const char *HTTP_STR="http://";
const char* HTTP_REQUEST_HEADER= //HTTP请求头


"GET %s HTTP/1.1\r\nAccept:*/*\r\n\
Accept-Language:zh-cn\r\n\
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
Host:%s\r\n\r\n";


bool initargs(SearchInfo &outinfo,const char*city)
{
memset(&outinfo,0,sizeof(SearchInfo));//初始化

char *temp=new char[256];
memset(temp,0,256);
sprintf(temp,URL,city);
printf(temp);
temp=(strstr(temp,HTTP_STR)!=NULL)?temp+strlen(HTTP_STR):temp;//去掉前面的http:\\
// printf("Url:%s\n",temp);
strcpy(outinfo.filename,strstr(temp,"/")!=NULL?strstr(temp,"/"):"/");//分析出要下载文件名
int length=strstr(temp,"/")==NULL?strlen(temp):(strlen(temp)-strlen(strstr(temp,"/")));
//分析出主机名的长度www.zzti.edu.cn/index.html-/index.html
memcpy(outinfo.host,temp,length);//解析出主机名
if((temp=strstr(outinfo.host,":"))!=NULL)//解析端口
{
temp++;
outinfo.port=atoi(temp);
}
else//如果没有输入使用默认80
{
outinfo.port=80;
}
delete temp;
return true;

}


char* GetFile(const SearchInfo * psi)
{
if(psi==NULL)
{
return NULL;
}
unsigned long serverip=0;//服务器IP
if((serverip=inet_addr(psi->host))==INADDR_NONE)//如果主机名不是IP
{
hostent *phst=gethostbyname(psi->host);//用DNS解析主机IP
if(phst==NULL)//如果解析失败返回false
return NULL;
//IN_ADDR in;
if(phst->h_addr_list[0]!=0)//解析成功使用主机第一个IP
{
memcpy(&serverip,phst->h_addr_list[0],phst->h_length);
//in.S_un.S_addr=serverip;
}
//printf("IP:%s",inet_ntoa(in));
}
SOCKET s=socket(AF_INET,SOCK_STREAM,0);//创建socket(TCP连接)
if(s==INVALID_SOCKET)//创建失败
{
printf("Create socket Error!Error Code:%d\n",WSAGetLastError());
return NULL;
}
SOCKADDR_IN server_addr;//服务器address
server_addr.sin_addr.S_un.S_addr=serverip;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(psi->port);
memset(server_addr.sin_zero,0,sizeof(server_addr.sin_zero));
printf("Begin Connect Server :%s On:%d\n",inet_ntoa(server_addr.sin_addr),psi->port);
//开始连接服务器发出请求
if(SOCKET_ERROR==connect(s,(const sockaddr*)&server_addr,sizeof(SOCKADDR_IN)))
{
printf("Connect Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//如果连接失败
return NULL;
}
printf("Connect Server OK!\n");
char buffer_sendmsg[256]={0};
sprintf(buffer_sendmsg,HTTP_REQUEST_HEADER,psi->filename,psi->host);//构造HTTP请求
//printf(buffer_sendmsg);
if(send(s,buffer_sendmsg,256,0)==SOCKET_ERROR)//向服务器发送请求
{
printf("Send Request To Server Error!Error Code:%d\n",WSAGetLastError());
closesocket(s);
//发送失败
return NULL;
}
//打开文件开始保存html代码

int len=0;
char buffer_recv[1024]={0};//接收html的buffer

int sumlen=0;//html的长度
char *html=(char*)malloc(sizeof(char)*1);//总的html字符串

while((len=recv(s,buffer_recv,1024,0))!=0)
{
if(len==SOCKET_ERROR)
{
printf("Error in Recv Data!Error Code:%d\n",WSAGetLastError());
closesocket(s);

return NULL;
}
sumlen=strlen(html);
printf(buffer_recv);


//重新分配内存原来大小加len长度
if((html=(char*)realloc((void*)html,sumlen+sizeof(char)*len))!=NULL)
{
memset(html+sumlen-1,0,len);//将新分到内存初始为0
strcat(html,buffer_recv);//将收到信息写入新分到内存
}
memset(buffer_recv,0,1024);
}

closesocket(s);
closesocket(s);

return strlen(html)==0?NULL:html;
}


void PrintWeather(const Weather_Info* pwi)
{
system("cls");
printf("\n");
printf("\n");
printf("\n");
printf(" \t 天气预报\n");
printf(" \t=====================================\n");
printf(" \t城 市:%s\n",pwi->city);
printf(" \t------------------------------------\n");
printf(" \t天 气:%s\n",pwi->weather);
printf(" \t------------------------------------\n");
printf(" \t温 度:%s\n",pwi->temp);
printf(" \t------------------------------------\n");
printf(" \t风 向:%s\n",pwi->wind);
printf(" \t======================================\n");
}


int main(int argc,char**argv)
{
Weather_Info wi;
SearchInfo si;
memset(&wi,0,sizeof(Weather_Info));
strcpy(wi.city,"郑州");
if(argc>=2)
strcpy(wi.city,argv[1]);
if(!initsocket())
{
printf("Socket Error~\n");
return 1;
}

memset(&si,0,sizeof(SearchInfo));
sprintf(si.host,URL,wi.city);
if(!initargs(si,wi.city))
{
printf("Connect Internet Error~\n");
return 1;
}
char *html=NULL;
if((html=GetFile(&si))==NULL)
{
printf("Get Information Error~!\n");
return 1;
}

if(!GetWeather(wi,html))
{
printf("Get Weather Info Error~\n");
return 1;
}Sleep(30000);
free(html);
PrintWeather(&wi);

system("pause");
return 0;
}
[解决办法]
这个是个SOAP的webservice吗?用gsoap之类的库直接调用吧。。
[解决办法]
用gSOAP获取天气预报
http://www.cppprog.com/2009/0723/138_2.html
[解决办法]
mark
[解决办法]
要是我的话,,就用python写。多方便啊。。。。。
[解决办法]

引用:
要是我的话,,就用python写。多方便啊。。。。。

那确实。。以前在linux搞过一个有关天气的。。
   
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib2
#57083是郑州的代码,如果你要使用此脚步,需要修改成你所在城市的代码
#可以在http://wap.weather.com.cn/wap/search/上查询到(url)
url = 'http://wap.weather.com.cn/wap/57083/h24/'
req = urllib2.Request(url)
fd = urllib2.urlopen(req)
data = fd.read(2048)
index = data.find("天气:")
result = data[index+9:data.find("\n",index)]
index = data.find("气温:")
result +=data[index+9:data.find("\n",index)].replace("度","°C").replace("到","-") + " "
index = data.find("风力:")
result += data[index+9:data.find("\n",index)]


print result


[解决办法]
d
[解决办法]
100分提问:在VC++中怎么获取天气预报温度信息并保存为txt(有源码)
[解决办法]
分太少
[解决办法]
顶。。

[解决办法]

[解决办法]
d
[解决办法]
up
[解决办法]
#include "stdafx.h"
#include <iostream>
#include <winsock.h>
#include <string>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

void main()
{
struct hostent *pURL;
struct sockaddr_in addr;
WSADATA WSAData={0};
SOCKET sockfd;
string header;
static char text[BUFSIZ];

WSAStartup(MAKEWORD(2,2), &WSAData);
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
pURL = gethostbyname("www.webxml.com.cn");
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr);
addr.sin_port = htons(80);
connect(sockfd,(SOCKADDR *)&addr,sizeof(addr));
header="GET ";
header.append("/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=57073");
header.append(" HTTP/1.1\r\n");
header.append("HOST: ");
header.append("www.webxml.com.cn");
header.append("\r\nConnection: Close\r\n\r\n");

send(sockfd, header.c_str(), strlen(header.c_str()), 0);

while (recv(sockfd, text, BUFSIZ, 0) != NULL)
{
cout<<text;
strnset(text, '\0', BUFSIZ);
}
closesocket(sockfd);
WSACleanup();
system("PAUSE");
}

[解决办法]
我帮不了~等有空的高手
[解决办法]
希望你早点回家过年
[解决办法]
引用:
玩去了,走音


嘿嘿 又错别字了。。。

LZ 此贴全给分给他就行了

读书人网 >C++

热点推荐