c语言获取直播吧最近一周nba比赛信息
就是用c语言的写的一个简单http请求,并分析其响应,原理很简单,主要是分析http响应,麻烦的是提取其中的比赛信息
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/mman.h>
#include <netdb.h> //struct hostent结构的头文件
#include <time.h>
#define USERAGENT "Wget/1.10.2"
#define ACCEPT "*/*"
#define ACCEPTLANGUAGE "zh-cn,zh;q=0.5"
#define ACCEPTENCODING "gzip,deflate"
#define ACCEPTCHARSET "gb2312,utf-8;q=0.7,*;q=0.7"
#define KEEPALIVE "300"
#define CONNECTION "keep-alive"
#define CONTENTTYPE "application/x-www-form-urlencoded"
#define HOSTNAME "www.zhibo8.cc"
#define DEBUG 1
char Request[409600]="";
char Recive[409600]="";
struct hostent *host;
int sockfd=0;
FILE *fp;
struct sockaddr_in server_addr;
int year;
int mon;
int day;
int wday;
int count=0;
void HandLine(char *l);
void SaveResult();
void GetDayInfo();
void GetTime();
int GetLocalAgent(char * UserAgent, char * Accept, char * AcceptLanguage, char * AcceptEncoding, char * AcceptCharset, char * KeepAlive, char * Connection, char * ContentType)
{
memcpy(UserAgent, USERAGENT, strlen(USERAGENT));
memcpy(Accept, ACCEPT, strlen(ACCEPT));
memcpy(AcceptLanguage, ACCEPTLANGUAGE, strlen(ACCEPTLANGUAGE));
memcpy(AcceptEncoding, ACCEPTENCODING, strlen(ACCEPTENCODING));
memcpy(AcceptCharset, ACCEPTCHARSET, strlen(ACCEPTCHARSET));
memcpy(KeepAlive, KEEPALIVE, strlen(KEEPALIVE));
memcpy(Connection, CONNECTION, strlen(CONNECTION));
memcpy(ContentType, CONTENTTYPE, strlen(CONTENTTYPE));
return 0;
}
void CreatRequest()
{
char UserAgent[1024] = "", Accept[1024] = "", AcceptLanguage[1024] = "", AcceptEncoding[1024] = "", AcceptCharset[1024] = "", KeepAlive[1024] = "", Connection[1024] = "", ContentType[1024] = "";
if((host=gethostbyname("www.zhibo8.cc"))==NULL) /* get ip address by domain */
{
if(DEBUG) fprintf(stderr,"\tGethostname '%s' error, %s\n", HOSTNAME, strerror(errno));
exit(1);
}
GetLocalAgent(UserAgent, Accept, AcceptLanguage, AcceptEncoding, AcceptCharset, KeepAlive, Connection, ContentType); /* Get client browser information */
sprintf(Request, "GET /%s/%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: %s\r\nConnection: %s\r\n\r\n", "/", "", HOSTNAME, UserAgent, Accept, Connection);
}
void ConnectWeb()
{
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"\t SOCKET ERROR:%s\a\n",strerror(errno));
exit(1);
}
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
if(connect(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"\t CONNECT ERROR:%s\a\n",strerror(errno));
exit(1);
}
}
void SendRequest()
{
int wn=0;
wn=write(sockfd,Request,strlen(Request));
if(wn==-1)
{
fprintf(stderr,"\t SEND ERROR:%s\a\n",strerror(errno));
exit(1);
}
fprintf(stdout, "\n\tRequest. send OK!\n" );
}
void SaveResult()
{
fd_set readfds;
struct timeval tival;
int i,j,ret;
i=j=ret=0;
int n=0;
RECIVE:
FD_ZERO(&readfds);
tival.tv_sec=10;
tival.tv_usec=0;
if(sockfd>0)
FD_SET(sockfd,&readfds);
ret=select(sockfd+1,&readfds,NULL,NULL,&tival);
if(ret==0)
{
if(n++<2)
goto RECIVE;
}
if(FD_ISSET(sockfd,&readfds))
{
/*读取全部相应内容到本地文件*/
while(read(sockfd,Recive,sizeof(Recive)))
{
fwrite(Recive,sizeof(Recive),1,fp);
memset(Recive,0,sizeof(Recive));
}
}
}
void EchoResult()
{
fd_set readfds;
struct timeval tival;
int i,j,ret;
i=j=ret=0;
int n=0;
RECIVE:
FD_ZERO(&readfds);
tival.tv_sec=2;
tival.tv_usec=0;
if(sockfd>0)
FD_SET(sockfd,&readfds);
ret=select(sockfd+1,&readfds,NULL,NULL,&tival);
if(ret==0)
{
if(n++<2)
goto RECIVE;
}
if(FD_ISSET(sockfd,&readfds))
{
char c;
while(read(sockfd,&c,1))
{
if(c=='d')
{
read(sockfd,&c,1);
if(c=='i')
{
read(sockfd,&c,1);
if(c=='v')
{
read(sockfd,&c,1);
if(c==' ')
{
read(sockfd,&c,1);
if(c=='c')
{
read(sockfd,&c,1);
if(c=='l')
{
read(sockfd,&c,1);
if(c=='a')
{
read(sockfd,&c,1);
if(c=='s')
{
read(sockfd,&c,1);
if(c=='s')
{
read(sockfd,&c,1);
if(c=='=')
{
read(sockfd,&c,1);
if(c=='"')
{
read(sockfd,&c,1);
if(c=='t')
{
read(sockfd,&c,1);
if(c=='i')
{
read(sockfd,&c,1);
if(c=='t')
{
read(sockfd,&c,1);
if(c=='l')
{
read(sockfd,&c,1);
if(c=='e')
{
read(sockfd,&c,1);
if(c=='b')
{
read(sockfd,&c,1);
if(c=='a')
{
read(sockfd,&c,1);
if(c=='r');
{
read(sockfd,&c,1);
if(c=='"')
GetDayInfo();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
void GetDayInfo()
{
// int f=open("./s.txt",O_WRONLY|O_CREAT);
char t;
char Line[1024]="";
int sday=wday+count;
if(count++>=7)
return;
printf("\n***********%d年 %d月 %d日, 星期 %d **********************\n\n",year,mon,day+count-1,sday>7?sday%7:sday);
while(read(sockfd,&t,1)) /*处理每天的比赛信息即<ul></ul>里的内容*/
{
if(t=='l')
{
read(sockfd,&t,1);
if(t=='i');
{
int ln=0;
memset(Line,0,sizeof(Line));
while(read(sockfd,Line+(ln++),1))/*处理每行的信息<li></li>里的内容*/
{
char *s=strstr(Line,"</li>");
if(s!=NULL)
break;
}
Line[ln]=0;
HandLine(Line);
}
}
else if(t=='/')
{
read(sockfd,&t,1);
if(t=='u')
{
read(sockfd,&t,1);
if(t=='l');
return;
}
}
}
}
void HandLine(char *l)
{
l++;
char *NBA="NBA常规赛";
char *p=strstr(l,NBA);
if(p==NULL)
return;
char *t=strstr(l,"<b");
int lt=strlen(l)-strlen(t);
char *time=(char*)malloc(sizeof(char)*lt);
memcpy(time,l,lt);
char *p1=strstr(p,"/b>");
char *p2=strstr(p,"<a");
int n=strlen(p1)-strlen(p2)-3;
char *TeamVsTeam=(char*)malloc(n*sizeof(char));
memcpy(TeamVsTeam,p1+3,n);
char *p3=strstr(p2,"_blank\">");
char *p4=strstr(p3,"</a>");
int m=strlen(p3)-strlen(p4)-8;
char *Live=(char*)malloc(sizeof(char)*m);
memcpy(Live,p3+8,m);
printf("time:%s The Teams:%s TV Live:%s\n",time,TeamVsTeam,Live);
}
void GetTime()
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
year=timeinfo->tm_year+1900;
mon =timeinfo->tm_mon+1;
day =timeinfo->tm_mday;
wday=timeinfo->tm_wday;
if(wday==0)
wday=7;
}
int main()
{
fp=fopen("Info.txt","w");
if(fp==NULL)
{
fprintf(stderr,"creat file is error \n");
exit(1);
}
CreatRequest();
ConnectWeb();
SendRequest();
EchoResult();
printf("IS END !!\n");
return 0;
}
上一篇:linux中查询dns示例
栏 目:C语言
本文标题:c语言获取直播吧最近一周nba比赛信息
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/3678.html
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言用函数写分段 用c语言表示分段函数
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段函数
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 04-02c语言调用函数求fibo C语言调用函数求阶乘
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10C#中split用法实例总结