##天气预报爬虫 项目

cs 复制代码
//主要功能 

#include "head.h"
#include "cJSON.h"


void FunWeather(void);
void RecvSendWeather(void);
int RealTimeWeather(void);
int CreateTcpClient(char *p,int port);
int SendHttpRequest(int sockfd,char *q);
void RecvSendWeather(void);

char city[256] = {0};
int sockfd = 0;

typedef struct media
{
	int id;
	char menulist[512];//菜单名字的长度
}MEDIA_CFG_S;

typedef struct menuinfo
{
	int id;
	int pos_start;
	int pos_end;
	int focus;

}MENU_CFG_S;

MEDIA_CFG_S medialist[32]=
{
	{1,"选择城市"},               
	{2,"实时天气"},               
	{3,"未来天气"},               
	{4,"历史天气"},               
	{5,"退出"},               
};

int pos_start=0;		//界面起始位置
int pos_end = 4;	//界面结束位置
int focus;			//界面焦点位置

void PrintfMenu(void)
{
	int i =0;
	printf("+-----------------------------+\n");
	printf("|---------天气预报系统--------|\n");
	
		for(i = pos_start;i<= pos_end;i++)
		{
			if(i == focus)
			{
				printf("|                             |\r|\033[30;43m%2d.%-20s\033[0m\n",medialist[i].id,medialist[i].menulist);
			}else 
			{
				printf("|                             |\r|%2d.%-20s\n",medialist[i].id,medialist[i].menulist);
			}
		}
		printf("+-----------------------------+\n");
		return;
}


void MenuChoose(void)
{	
	char tmpbuff[1024]={0};

	char w[10] = {0};
	memset(w,0,sizeof(w));
	fgets(w,sizeof(w),stdin);
	
	if(27 == w[0] && 10 == w[1])//esc
	{
		
	}
	if(10 == w[0] && focus ==4)//回车
	{
		exit(1);
	}
	if(10 == w[0] && focus ==0)
	{	
		system("clear");
		printf("请输入要查询的城市:");  //选择城市
		scanf("%s",city);
		while(getchar()!= '\n');
//		SendWeather();
	
	}
	if(10 == w[0] && focus ==1)	//实时天气
	{
		system("clear");
//		memset(w,0,sizeof(w));
//		fgets(w,sizeof(w),stdin);
//		if(10 == w[0])
//		{
			CreateTcpClient("103.205.5.249",80);
			sprintf(tmpbuff,"/?app=weather.today&weaid=%s&appkey=72316&sign=f17235c2563dbbc6643d7a49d83fbc1d",city);
			SendHttpRequest(sockfd,tmpbuff);

			RecvSendWeather();
			RealTimeWeather();
	}
	
	if(10 == w[0] && focus ==2) //未来天气
	{
//		system("clear");
		
		CreateTcpClient("103.205.5.249",80);
		sprintf(tmpbuff,"http://api.k780.com/?app=weather.future&weaid=%s&appkey=72316&sign=f17235c2563dbbc6643d7a49d83fbc1d&format=json",city);
		SendHttpRequest(sockfd,tmpbuff);
		RecvSendWeather();
		FunWeather();
	}








	if(27 == w[0] && 91==w[1])
		{
			if(65 == w[2])//上
			{
				if(focus > pos_start)
				{
		system("clear");
					focus--;
				}else if(focus == pos_start && pos_start ==0)
				{
					return;
				}else if(focus == pos_start)
				{
		system("clear");
					pos_start--;
					pos_end--;
					focus--;
				}
			}else if(66 ==w[2]) //下
			{
				if(focus < pos_end)
				{
		system("clear");
					focus++;
				}else if(focus == pos_end && pos_end == 4)
				{
					return;
				}else if(focus == pos_end)
				{
		system("clear");
					pos_start++;
					pos_end++;
					focus++;
				}
			}
		}

}

int CreateTcpClient(char *p,int port) //创建一个TCP用户端
{
	int ret = 0;
	struct sockaddr_in seraddr;

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1==sockfd)
	{
		perror("fail to socket");
		return -1;
	}

	seraddr.sin_family = AF_INET;
	seraddr.sin_port = htons(port);
	seraddr.sin_addr.s_addr = inet_addr(p);
	
	ret = connect(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr));
	if(-1==ret)
	{
		perror("fail to connect");
		return -1;
	}

	return 0;
}

int SendHttpRequest(int sockfd,char *q)  //向http里面发送 
{
	char tmpbuff[4096]={0};
	ssize_t nsize = 0;

	sprintf(tmpbuff,"GET %s HTTP/1.1\r\n",q);
	sprintf(tmpbuff,"%sHost: api.k780.com\r\n",tmpbuff);
	sprintf(tmpbuff,"%sUser-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0\r\n",tmpbuff);
	sprintf(tmpbuff,"%sAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\r\n",tmpbuff);
	sprintf(tmpbuff,"%sAccept-Language: en-US,en;q=0.5\r\n",tmpbuff);
	sprintf(tmpbuff,"%sConnection: keep-alive\r\n\r\n",tmpbuff);

	nsize = send(sockfd,tmpbuff,strlen(tmpbuff),0);

	return 0;
}


int RealTimeWeather(void)
{
	int fd = 0;
	char tmpbuff[1000000] = {0};

	fd = open("recv.txt",O_RDONLY);

	cJSON *proot = NULL;
	cJSON *pvalue = NULL;
	cJSON *ptmp = NULL;

	read(fd,tmpbuff,sizeof(tmpbuff));

	proot = cJSON_Parse(tmpbuff);
	if(NULL == proot)
	{
		fprintf(stderr,"creat root json failed!\n");
	}

	pvalue = cJSON_GetObjectItem(proot,"result");
	if(NULL == pvalue)
	{
		fprintf(stderr,"can not find result\n");
		return -1;
	}

	ptmp = cJSON_GetObjectItem(pvalue,"days");
	printf("时间:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"week");
	printf("星期:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"citynm");
	printf("地点:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"temperature_curr");
	printf("当前温度:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"temp_high");
	printf("最高温度:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"temp_low");
	printf("最低温度:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"weather");
	printf("天气:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"humidity");
	printf("当前湿度:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"humi_high");
	printf("最大湿度:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"humi_low");
	printf("最小湿度:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"temperature");
	printf("白天 夜间温度:%s\n",ptmp->valuestring);

	ptmp = cJSON_GetObjectItem(pvalue,"aqi");
	printf("pm2.5:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"wind");
	printf("风向:%s\n",ptmp->valuestring);
	
	ptmp = cJSON_GetObjectItem(pvalue,"winp");
	printf("风力:%s\n",ptmp->valuestring);
	
	close(fd);
	cJSON_Delete(proot);
}

void FunWeather(void)
{
	cJSON *proot = NULL;
	cJSON *pvalue = NULL;
	cJSON *ptmp = NULL;
	cJSON *p = NULL;
	int fd = 0;
	int i = 0;
	char tmpbuff[4096] = {0};

	fd = open("recv.txt",O_RDONLY);

	read(fd,tmpbuff,sizeof(tmpbuff));

	proot = cJSON_Parse(tmpbuff);
	if(NULL == proot)
	{
		fprintf(stderr,"creat root json failed!\n");
	}

	pvalue = cJSON_GetObjectItem(proot,"result");
	if(NULL == pvalue)
	{
		fprintf(stderr,"can not find result\n");
	//	return -1;
	}

	for( i = 0;i < cJSON_GetArraySize(pvalue);++i)
	{
		ptmp = cJSON_GetArrayItem(pvalue,i);
		if(NULL == ptmp)
		{
			fprintf(stderr, "can not find location json struct %d item\n", i);
	//		return -1;
		}
		p = cJSON_GetObjectItem(ptmp,"days");
		printf("时间:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"week");
		printf("星期:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"citynm");
		printf("地点:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"weather");
		printf("天气:%-20s",p->valuestring);

		putchar('\n');

		p = cJSON_GetObjectItem(ptmp,"temperature");
		printf("白天 夜间温度:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"temp_high");
		printf("白天温度:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"temp_low");
		printf("夜间温度:%-20s",p->valuestring);

		putchar('\n');

		p = cJSON_GetObjectItem(ptmp,"wind");
		printf("风向:%-20s",p->valuestring);

		p = cJSON_GetObjectItem(ptmp,"winp");
		printf("风力:%-20s",p->valuestring);

		putchar('\n');
	}

	close(fd);
	cJSON_Delete(proot);
}
void RecvSendWeather(void)
{	
	int fd = 0;
	char buff[30000];
	char cmpbuff[30000];
	char tmpbuff[4096]={0};
	ssize_t nsize = 0;
	char *ptmp = NULL;
	char *pstart = NULL;
	char *pend =NULL;
	
	fd = open("recv.txt",O_RDWR|O_CREAT|O_TRUNC,0664);

	while(1)
	{
		memset(tmpbuff,0,sizeof(tmpbuff));
		nsize = recv(sockfd,tmpbuff,sizeof(tmpbuff),0);
		
		strcat(buff,tmpbuff);
		if(strstr(tmpbuff,"0\r\n") != NULL)
		{
			break;
		}
	}
	
	ptmp = strstr(buff,"\r\n\r\n");
	ptmp += 4;
	ptmp = strstr(ptmp,"\r\n");
	ptmp += 2;
	pstart = ptmp;
	pend = strstr(ptmp,"\r\n");

	memset(cmpbuff,0,sizeof(cmpbuff));
	strncpy(cmpbuff,pstart,pend - pstart);
	
	write(fd,cmpbuff,strlen(cmpbuff));

/*
	sockfd = CreateTcpClient("103.205.5.249",80);
	SendHttpRequest(sockfd,"/?app=weather.today&weaid=西安&appkey=44923&sign=c9815919d111da6c2c9ca64a304f640b&format=json");
	
	nsize = recv(sockfd,tmpbuff,sizeof(tmpbuff),0);

	printf("================================= RECV ================================\n");
	printf("%s\n", tmpbuff);
	printf("=======================================================================\n");
*/
	close(sockfd);	
	close(fd);
}


int main(void)
{	
	while(1)
	{
//		system("clear");
		PrintfMenu();
		MenuChoose();
	}
	return 0;
}
相关推荐
全栈开发圈17 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
小白学大数据22 分钟前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
ac-er88881 小时前
PHP网络爬虫常见的反爬策略
开发语言·爬虫·php
聪明的墨菲特i4 小时前
Python爬虫学习
爬虫·python·学习
oliveira-time6 小时前
爬虫学习6
爬虫
xiaoxiongip66611 小时前
HTTP 和 HTTPS
网络·爬虫·网络协议·tcp/ip·http·https·ip
兆。13 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
API快乐传递者19 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
兜里有糖请分享1 天前
Python中序列化/反序列化JSON格式的数据
爬虫·python
亿牛云爬虫专家1 天前
用Puppeteer点击与数据爬取:实现动态网页交互
javascript·爬虫·爬虫代理·puppeteer·数据·代理ip·16yun