##天气预报爬虫 项目

news2024/7/4 6:07:10
//主要功能 

#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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1507579.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Socket通信Demo(Unity客户端和C#)

新建一个Unity项目新建脚本编写客户端 using System.Net.Sockets; using System.Net; using System; using System.Text;public class Client : MonoBehaviour {private Socket socket;//定义用来存消息的容器private byte[] buffer new byte[1024];// Start is called befor…

国际妇女节 | 竹云董事长董宁在第五届大湾区木棉论坛主题演讲

3月7日-8日&#xff0c;由深圳市妇女联合会指导&#xff0c;深商会联合党委主办的第五届大湾区木棉论坛在深商报告厅圆满举行。 本次论坛以“木棉盛开”为主题&#xff0c;邀请深圳市妇联党组书记、主席刘蕾&#xff0c;深圳市霖峰投资董事长王秀娥&#xff0c;深圳市国富黄金股…

速卖通商品采集API:关键字搜索商品item_search、获取商品详情item_get

item_get-获得aliexpress商品详情 item_search-按关键字搜索aliexpress商品 公共参数 请求地址: aliexpress.item_search/aliexpress.item_get 名称类型必须描述keyString是调用key&#xff08;必须以GET方式拼接在URL中&#xff09;secretString是调用密钥api_nameString是…

Buran勒索病毒通过Microsoft Excel Web查询文件进行传播

Buran勒索病毒首次出现在2019年5月&#xff0c;是一款新型的基于RaaS模式进行传播的新型勒索病毒&#xff0c;在一个著名的俄罗斯论坛中进行销售&#xff0c;与其他基于RaaS勒索病毒(如GandCrab)获得30%-40%的收入不同&#xff0c;Buran勒索病毒的作者仅占感染产生的25%的收入,…

Unity的滑动控制相机跟随和第三人称视角三

Unity的相机跟随和第三人称视角三 第三人称相机优化介绍讲解拖动事件相机逻辑人物移动逻辑总结 第三人称相机优化 Unity第三人称相机视角一 Unity第三人称相机视角二 介绍 之前相机视角讲过了两篇文章了&#xff0c;但是都是自动旋转视角&#xff0c;今天来了新需求&#xf…

FPGA高端项目:FPGA基于GS2971的SDI视频接收+HLS多路视频融合叠加,提供1套工程源码和技术支持

目录 1、前言免责声明 2、相关方案推荐本博已有的 SDI 编解码方案本方案的SDI接收转HDMI输出应用本方案的SDI接收图像缩放应用本方案的SDI接收纯verilog图像缩放纯verilog多路视频拼接应用本方案的SDI接收HLS图像缩放Video Mixer多路视频拼接应用本方案的SDI接收OSD动态字符叠加…

Day23:安全开发-PHP应用后台模块SessionCookieToken身份验证唯一性

目录 具体安全知识点 身份验证-Cookie使用 身份验证-Session使用 唯一性判断-Token使用 总结 源码 思维导图 PHP知识点&#xff1a; 功能&#xff1a;新闻列表&#xff0c;会员中心&#xff0c;资源下载&#xff0c;留言版&#xff0c;后台模块&#xff0c;模版引用&…

Java 拦截器Interceptor详解

1、拦截器概念 先看一下 3、代码示例 4、使用类解读 5、使用注解解读 6、与filter区别 7、优化策略 8、常见问题分析 学海无涯苦作舟&#xff01;&#xff01;&#xff01;

无尘擦拭布:保持清洁,确保品质

在现代工业和科技领域中&#xff0c;保持工作环境的清洁和设备的无尘是至关重要的。无尘布作为一种重要的清洁工具&#xff0c;在这一领域发挥着不可或缺的作用。它具有一系列优秀的特性&#xff0c;使其成为各种行业中的*选之一。 优秀的除尘效果与防静电功能 首先&#xff0…

Jupyter Notebook使用教程——从Anaconda环境构建到Markdown、LaTex语法介绍

0. 前言 按照国际惯例&#xff0c;首先声明&#xff1a;本文只是我自己学习的理解&#xff0c;虽然参考了他人的宝贵见解及成果&#xff0c;但是内容可能存在不准确的地方。如果发现文中错误&#xff0c;希望批评指正&#xff0c;共同进步。 你是否在视频教程或说明文档或Githu…

ejs模版引擎使用

ejs express渲染页面需要借助ejs官网传送门使用流程 安装ejs引擎 npm i ejs -S 在app.js入口文件里面设置模版引擎和目录 const path require(path)// 设置项目里面模版存放位置app.set(views, path.join(__dirname, views))// 设置模版引擎app.set(view engine, ejs)在项…

重学SpringBoot3-路径匹配机制

重学SpringBoot3-路径匹配机制 AntPathMatcherPathPatternParser 和 PathPattern演示AntPathMatcher 示例PathPattern 示例性能和精确度的提升 选择使用哪一种 在 Spring Framework 5.3 及 Spring Boot 2.4 之后&#xff0c;引入了一种新的路径匹配机制&#xff0c;这一变化在 …

Buildroot 之一 详解源码及架构

在之前的博文中,我们学习了直接通过 Makefile 手动来进行构建 U-Boot 和 Linux Kernel 等,其实,目前存在多种嵌入式 Linux 环境的构建工具,其中,Buildroot 就是被广泛应用的一种。今天就来详细学习一个 Buildroot 这个自动化构建工具。 Buildroot Buildroot 是一个运行于…

福派斯课堂:怎样选择品质比较好的猫粮?

亲爱的猫友们&#xff0c;我们都知道&#xff0c;猫咪的健康离不开好的饮食&#xff0c;而猫粮作为它们日常的主食&#xff0c;选择一款品质优良的猫粮就显得尤为重要了。那么&#xff0c;如何选择品质比较好的猫粮呢&#xff1f;今天&#xff0c;就让我来给大家支支招吧&#…

PaddlePaddle框架安装

提示&#xff1a;可在python环境中进行安装&#xff0c;避免环境污染&#xff0c;创建命令conda create -n xxx_name python3.9,激活conda activate xxx_name 第一步&#xff1a;查看计算机平台版本 在窗口输入查看命令&#xff0c;查看CUDA的版本 nvidia-smi 二、根据以下条件…

Redis主从架构和管道Lua(一)

Redis主从架构 架构 Redis主从工作原理 如果为master配置了一个slave,不管这个slave是否是第一次连接上Master,它都会发送一个PSYNC命令给master请求复制数据。master受到PSYNC命令&#xff0c;会在后台进行数据持久化通过bgsave生成最新的 RDB快照文件&#xff0c;持久化期间…

Linux之线程控制

目录 一、POSIX线程库 二、线程的创建 三、线程等待 四、线程终止 五、分离线程 六、线程ID&#xff1a;pthread_t 1、获取线程ID 2、pthread_t 七、线程局部存储&#xff1a;__thread 一、POSIX线程库 由于Linux下的线程并没有独立特有的结构&#xff0c;所以Linux并…

[Mac软件]Adobe Illustrator 2024 28.3 intel/M1/M2/M3矢量图制作软件

应用介绍 Adobe Illustrator 是行业标准的矢量图形应用程序&#xff0c;可以为印刷、网络、视频和移动设备创建logos、图标、绘图、排版和插图。数以百万计的设计师和艺术家使用Illustrator CC创作&#xff0c;从网页图标和产品包装到书籍插图和广告牌。 绘制任意大小的标志 拥…

C++进阶之路---多态(一)

顾得泉&#xff1a;个人主页 个人专栏&#xff1a;《Linux操作系统》 《C从入门到精通》 《LeedCode刷题》 键盘敲烂&#xff0c;年薪百万&#xff01; 一、多态的概念 1.概念 多态的概念&#xff1a;通俗来说&#xff0c;就是多种形态&#xff0c;具体点就是去完成某个行为…

[gic]-linux和optee的中断处理流程举例(gicv3举例)

目录 环境配置:说明:举例1、当cpu处于REE&#xff0c;来了一个非安全中断2、当cpu处于TEE&#xff0c;来了一个安全中断3、当cpu处于TEE&#xff0c;来了一个非安全中断4、当cpu处于REE&#xff0c;来了一个安全中断答疑 推荐 本文转自 周贺贺&#xff0c;baron&#xff0c;代码…