-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.cpp
268 lines (221 loc) · 4.48 KB
/
crawler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "crawler.h"
#include "regex.h"
#include <sys/time.h>
static void * mymalloc()
{
}
char* itoa(int num,char*str,int radix)
{/*索引表*/
char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned unum;/*中间变量*/
int i=0,j,k;
/*确定unum的值*/
if(radix==10&&num<0)/*十进制负数*/
{
unum=(unsigned)-num;
str[i++]='-';
}
else unum=(unsigned)num;/*其他情况*/
/*转换*/
do{
str[i++]=index[unum%(unsigned)radix];
unum/=radix;
}while(unum);
str[i]='\0';
/*逆序*/
if(str[0]=='-')k=1;/*十进制负数*/
else k=0;
char temp;
for(j=k;j<=(i-1)/2;j++)
{
temp=str[j];
str[j]=str[i-1+k-j];
str[i-1+k-j]=temp;
}
return str;
}
void get_and_prase_html(Curl &murl,Url &tmp,MySqlServer &mySql)
{
if(tmp.depth>=MAX_DEPTH)
{
return;
}
char *url=(char *)malloc(1024);
assert(url!=NULL);
strcpy(url,tmp.url.c_str());
char *domainname=get_domainname(url);
int sockfd=creat_sock(domainname);
if(sockfd==-1)
{
free(domainname);
free(url);
return ;
}
sendpost(domainname,url,sockfd);
int recvsize=0;
char *recvbuff=recvpost(sockfd,&recvsize);
printf("recvbuff size=%d\n",recvsize);
if(recvsize==0)
{
free(recvbuff);
free(url);
return ;
}
saveurl(recvbuff,murl,tmp.depth);
savehtml(recvbuff,recvsize,url,mySql);
free(url);
}
bool get_ip_byname(char *domainname,char *hostip)
{
if(domainname == NULL)
{
printf("domainname is NULL\n");
return false;
}
struct hostent *host;
if((host = gethostbyname(domainname)) == NULL)
{
printf("can not get it\n");
return false;
}
else
{
strcpy(hostip,inet_ntoa(*((struct in_addr *)host->h_addr)));
return true;
}
}
int creat_sock(char *domainname)
{
int sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sockfd==-1)
{
perror("socket error");
return -1;
}
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(80);
char hostip[32] = "";
if(get_ip_byname(domainname,hostip))
{
//printf("host:%s\n",domainname);
//printf("ip :%s\n\n",hostip);
saddr.sin_addr.s_addr = inet_addr(hostip);
}
else
{
printf("get ip error \n");
return -1;
}
struct timeval ntime={5,0};
setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(char *)&ntime,sizeof(struct timeval));
int res = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
if(res == -1)
{
perror("connect error");
close(sockfd);
return -1;
}
return sockfd;
}
void makepost(char *domainname,char *url,char *postbuff)
{
sprintf(postbuff,"GET %s HTTP/1.1\r\nHost: ",url);
strcat(postbuff,domainname);
strcat(postbuff, "\r\nConnection:Close\r\n\r\n");
}
void sendpost(char *domainname,char *url,int sockfd)
{
int size = sizeof(char)*1024;
char *postbuff = (char *)malloc(size);
assert(postbuff != NULL);
makepost(domainname,url,postbuff);
if(send(sockfd,postbuff,strlen(postbuff),0) == -1)
{
printf("send error!\n\n");
}
printf("post sended :\n%s\n",postbuff);
free(postbuff);
free(domainname);
}
char *recvpost(int sockfd,int *msize)
{
int size = sizeof(char)*1024*1024*2;
char *recvbuff = (char *)malloc(size);
assert(recvbuff!=NULL);
char *tmp=recvbuff;
assert(recvbuff != NULL);
memset(recvbuff,0,size);
int ret=0;
while((ret=recv(sockfd,recvbuff,size,0)) > 0)
{
recvbuff=recvbuff+ret;
*msize+=ret;
}
printf("recv seccess,saving html...\n");
close(sockfd);
return tmp;
}
int openfile(int ret)
{
char tmp[12]={0};
itoa(ret,tmp,10);
char path[128]="/home/wyq/cspider/html/";
strcat(path,tmp);
int fd=open(path,O_CREAT | O_WRONLY,0600);
assert(fd!=-1);
return fd;
}
bool savehtml(char *recvbuff,int size,char *path,MySqlServer &mySql)
{
int ret=mySql.insert_htmlfile(path);
int fw=openfile(ret);
char *tmp=recvbuff;
int i=0;
while(strncmp(recvbuff,"\r\n\r\n",4)!=0)
{
recvbuff++;
i++;
}
recvbuff=recvbuff+4;
i+=4;
if(write(fw,recvbuff,size-i) == -1)
{
printf("saving html error!\n\n");
return false;
}
close(fw);
free(tmp);
return true;
}
char * get_domainname(char *url)
{
char *domainname=(char *)malloc(128);
assert(domainname!=NULL);
int i= 0;
if(strncmp(url,"http://",7)==0)
{
url=url+7;
}
while((*url!='/')&&(*url!='\0'))
{
domainname[i++]=*url++;
}
domainname[i]='\0';
return domainname;
}
/*
int main(int argc,char **argv)
{
if(argc < 2)
{
printf("arguments of main are not enough\n");
exit(-1);
}
char *domainname=get_domainname(argv[1]);//fenliyuming
printf("%s\n",domainname);
int sockfd = creat_sock(domainname);//chuangjiantaojiezi
return 0;
}
*/