c语言获取直播吧最近一周nba比赛信息
本文导语: 就是用c语言的写的一个简单http请求,并分析其响应,原理很简单,主要是分析http响应,麻烦的是提取其中的比赛信息 代码如下:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #...
就是用c语言的写的一个简单http请求,并分析其响应,原理很简单,主要是分析http响应,麻烦的是提取其中的比赛信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //struct hostent结构的头文件
#include
#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, %sn", 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.0rnHost: %srnUser-Agent: %srnAccept: %srnConnection: %srnrn", "/", "", HOSTNAME, UserAgent, Accept, Connection);
}
void ConnectWeb()
{
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"t SOCKET ERROR:%san",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:%san",strerror(errno));
exit(1);
}
}
void SendRequest()
{
int wn=0;
wn=write(sockfd,Request,strlen(Request));
if(wn==-1)
{
fprintf(stderr,"t SEND ERROR:%san",strerror(errno));
exit(1);
}
fprintf(stdout, "ntRequest. 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++0)
FD_SET(sockfd,&readfds);
ret=select(sockfd+1,&readfds,NULL,NULL,&tival);
if(ret==0)
{
if(n++=7)
return;
printf("n***********%d年 %d月 %d日, 星期 %d **********************nn",year,mon,day+count-1,sday>7?sday%7:sday);
while(read(sockfd,&t,1)) /*处理每天的比赛信息即
{
if(t=='l')
{
read(sockfd,&t,1);
if(t=='i');
{
int ln=0;
memset(Line,0,sizeof(Line));
while(read(sockfd,Line+(ln++),1))/*处理每行的信息里的内容*/
{
char *s=strstr(Line,"");
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,"");
char *p2=strstr(p,"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;
}