C language Get live. The last week NBA game information _c language

Source: Internet
Author: User
Tags get ip goto http request socket error strlen

The C language is written in a simple HTTP request, and analysis of its response, the principle is very simple, mainly to analyze the HTTP response, the trouble is to extract the game information

Copy Code code as follows:

#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>
Header files for #include <netdb.h>//struct hostent structure
#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, Cha R * 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 addresses 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);
}

Copy Code code as follows:

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))
{
/* Read all the appropriate content to the local file * *
while (Read (sockfd,recive,sizeof (Recive))
{
Fwrite (Recive,sizeof (Recive), 1,FP);
memset (recive,0,sizeof (Recive));
}

}
}

Copy Code code as follows:

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

}
}

}

Copy Code code as follows:

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 months%d days, week%d **********************\n\n", year,mon,day+count-1,sday>7?sday%7:sday);
while (read (sockfd,&t,1))/* Handles daily game information that is <ul></ul> content * *
{

if (t== ' l ')
{
Read (sockfd,&t,1);
if (t== ' I ');
{
int ln=0;
memset (line) (line,0,sizeof);
while (Read (sockfd,line+ (ln++), 1))/* Handles the information <li></li> contents of each line * *
{

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 regular Game";
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.