c语言编程求nba比赛,c语言写的一个 获取直播吧上最近一周NBA的比赛信息

就是用c语言的写的一个简单http请求,并分析其响应,原理很简单,主要是分析http响应,麻烦的是提取其中的比赛信息,(头疼啊) 还在修改中!

c语言和网络都是初学中 求指教啊

1.[代码][C/C++]代码

#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, %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);

}

2.[代码][C/C++]代码

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));

}

}

}

3.[代码]这个写的好麻烦啊

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();

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

}

4.[代码][C/C++]代码

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)) /*处理每天的比赛信息即

{

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,"

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,"

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,"

");

int m=strlen(p3)-strlen(p4)-8;

char *Live=(char*)malloc(sizeof(char)*m);

memcpy(Live,p3+8,m);

printf("time:%sThe Teams:%sTV 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值