signed

QiShunwang

“诚信为本、客户至上”

c语言获取直播吧最近一周nba比赛信息

2021/6/9 8:31:56   来源:

就是用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;
}