linux环境下的网络爬虫


在linux环境下运行的开源网络爬虫系统,有具体使用步骤
资源截图
代码片段和文件信息
/* http.cpp
 * this file supports TCP socket operations. such as gethostbyname()
 * create socket etc.
 */

#include “http.h“

extern setSet;
extern URL url; 
extern queueque;
extern struct epoll_event events[31];
extern struct timeval t_st t_ed;
extern int epfd;
extern int cnt;
extern int sum_byte;
extern int pending;
extern int MAX_URL;
extern bool is_first_url;
extern double time_used;
extern pthread_mutex_t quelock;
extern pthread_mutex_t connlock;
struct hostent *Host;

/* GetHostByName()
 * by call gethostbyname() we can get host infomation
 * through domain
 */

int GetHostByName(const string& hname)
{
if((Host = gethostbyname(hname.c_str())) == NULL)
{
return -1;
}
return 1;
}

/* SetNoblocking()
 * set nonblocking IO model
 */

int SetNoblocking(const int& sockfd)
{
int opts = fcntl(sockfd F_GETFL);

if (opts < 0) 
{
return -1;
}

// set nonblocking
opts |= O_NONBLOCK;

if (fcntl(sockfd F_SETFL opts) < 0) 
{
return -1;
}
}

/* ConnectWeb()
 * this function used to create a new socket fd and
 * then connect to host
 */

int ConnectWeb(int& sockfd)
{
struct sockaddr_in server_addr;

// create socket
if((sockfd = socket(PF_INET SOCK_STREAM 0)) == -1) 
{
return -1;
}
#ifdef DEBUG
puts(“create socket ok“);
#endif

// initialize server_addr
bzero(&server_addr 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);

// connect to host
if(connect(sockfd (struct sockaddr *)(&server_addr) sizeof(struct sockaddr)) == -1)
{
perror(“connect error“);
return -1;
}
#ifdef DEBUG
puts(“connect ok“);
#endif

pthread_mutex_lock(&connlock);
pending++;
pthread_mutex_unlock(&connlock);
}

/* SendRequest()
 * this function used to send request to host. tell host
 * what you want to do.
 */

int SendRequest(int sockfd URL& url_t)
{
// initialize request
string request;
string Uagent = UAGENT Conn = CONN Accept = ACCEPT;
request = “GET /“ + url_t.GetFile() + “ HTTP/1.1
Host: “ + url_t.GetHost() + “
User-Agent: “ + 
  Uagent + “
Accept: “ + Accept + “
Connection: “ + Conn + “

“;
  
// write(send request)
int d total = request.length() send = 0;

while(send < total)
{
if((d = write(sockfd request.c_str()+send total-send)) < 0) 
{
return -1;
}
send += d;
}
#ifdef DEBUG
puts(“write in socket ok“);
#endif
}

/* Calc_Time_Sec()
 * this function used to calculate the diffrent time between
 * two time. the time is based on struct timeval:
 * struct timeval
 * {
 * __time_t tv_sec;        // Seconds.
 * __suseconds_t tv_usec;    // Microseconds. 
 * };
 */

double Calc_Time_Sec(struct timeval st struct timeval ed)
{
double sec = ed.tv_sec - st.tv_sec;
double usec = ed.tv_usec - st.tv_usec;

return sec + usec/1000000;
}

/* GetResponse()
 * receive the data from host. which will return page information.
 * if ok(get the Web page successfull

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-08-27 22:06  linux环境下的网络爬虫
     目录           0  2012-08-27 22:08  linux环境下的网络爬虫Net-Spider
     文件         610  2012-05-02 22:22  linux环境下的网络爬虫Net-Spiderconfig.h
     文件        7170  2012-04-27 23:13  linux环境下的网络爬虫Net-Spiderhttp.cpp
     文件         960  2012-04-22 23:06  linux环境下的网络爬虫Net-Spiderhttp.h
     文件       22288  2012-05-03 09:04  linux环境下的网络爬虫Net-Spiderhttp.o
     文件         229  2012-04-25 21:35  linux环境下的网络爬虫Net-SpiderMakefile
     目录           0  2012-05-03 09:04  linux环境下的网络爬虫Net-SpiderPages
     文件        1493  2012-05-02 22:25  linux环境下的网络爬虫Net-SpiderREADME
     文件       63443  2012-05-03 09:04  linux环境下的网络爬虫Net-Spiderspider
     文件        7649  2012-05-02 22:48  linux环境下的网络爬虫Net-Spiderspider.cpp
     文件       79164  2012-05-03 09:04  linux环境下的网络爬虫Net-Spiderspider.o
     文件        8845  2012-04-20 22:30  linux环境下的网络爬虫Net-Spider ags
     文件        4096  2012-04-23 23:32  linux环境下的网络爬虫Net-Spiderweb.cpp
     文件        1096  2012-04-18 22:09  linux环境下的网络爬虫Net-Spiderweb.h
     文件       45684  2012-05-03 09:04  linux环境下的网络爬虫Net-Spiderweb.o
     文件     7703627  2012-08-27 22:08  linux环境下的网络爬虫Net-Spider基于Linux C、C++平台的网络爬虫的研究与实现.doc

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)