Larbin源代码分析[10]IPSITE站点的抽象类
2011-10-24 17:39
155 查看
一 该类的功能
NamedSite中的类经过dns请求之后,就会将站点名转换为ip,然后将url transfer到Ipsite中,由Ipsite具体完成爬虫工作。
二 类的具体成员参数
time_t lastAccess; //该IP最后获取的时间
bool isInFifo; //当该对象完成了一次fetch之后,将其加入到oksites中,并将其置为true,或者将一个url加入进fifo中
url *getUrl (); //获取队列中的 url对象
IPSite ();
~IPSite ();
Fifo<url> tab; //队列中进行爬取的url列表
int fetch (); //具体执行爬取
三 具体函数实现
(1) 构造函数IPSite() 函数的具体实现为 lastAccess = 0 ; isInFifo = false ;
(2)IPSite::putUrl (url *u)
将url放入IPSite的fifo队列中,然后将isInFifo设置为true。如果lastAccess+waitDuration <= now
以及当期存在空闲的连接,则可以 调用fetch以进行爬取网页。
否则将该IpSite加入进okSites中。
(3)inline url *IPSite::getUrl () 从fifo中获取一个url
(4) fetch() 函数,用以爬取网页
Connexion *conn = global::freeConns->get(); //首先获取一个空闲的连接
url *u = getUrl(); //获取一个url
char res = getFds(conn, &(u->addr), u->getPort()); //然后根据url建立一个连接
if (res != emptyC) { //下面则为一个具体发送http报文请求的过程
lastAccess = global::now;
conn->timeout = timeoutPage;
conn->request.addString("GET ");
if (global::proxyAddr != NULL) {
char *tmp = u->getUrl();
conn->request.addString(tmp);
} else {
conn->request.addString(u->getFile());
}
conn->request.addString(" HTTP/1.0\r\nHost: ");
conn->request.addString(u->getHost());
#ifdef COOKIES
if (u->cookie != NULL) {
conn->request.addString("\r\nCookie: ");
conn->request.addString(u->cookie);
}
#endif // COOKIES
conn->request.addString(global::headers);
conn->parser = new html (u, conn); //建立页面解析类
conn->pos = 0; //获取的数据中的下标位置 ,pos
conn->err = success;
conn->state = res;
if (tab.isEmpty()) {
isInFifo = false;
} else {
global::okSites->put(this);
}
return 0;
}
下面附带上一次http请求的头部的格式
GET /index.html HTTP/1.1
Accept:text/html
User-Agent:larbin
Host: ioa.rising.com.cn
From:jiangzy@rising.com.cn
NamedSite中的类经过dns请求之后,就会将站点名转换为ip,然后将url transfer到Ipsite中,由Ipsite具体完成爬虫工作。
二 类的具体成员参数
time_t lastAccess; //该IP最后获取的时间
bool isInFifo; //当该对象完成了一次fetch之后,将其加入到oksites中,并将其置为true,或者将一个url加入进fifo中
url *getUrl (); //获取队列中的 url对象
IPSite ();
~IPSite ();
Fifo<url> tab; //队列中进行爬取的url列表
int fetch (); //具体执行爬取
三 具体函数实现
(1) 构造函数IPSite() 函数的具体实现为 lastAccess = 0 ; isInFifo = false ;
(2)IPSite::putUrl (url *u)
将url放入IPSite的fifo队列中,然后将isInFifo设置为true。如果lastAccess+waitDuration <= now
以及当期存在空闲的连接,则可以 调用fetch以进行爬取网页。
否则将该IpSite加入进okSites中。
(3)inline url *IPSite::getUrl () 从fifo中获取一个url
(4) fetch() 函数,用以爬取网页
Connexion *conn = global::freeConns->get(); //首先获取一个空闲的连接
url *u = getUrl(); //获取一个url
char res = getFds(conn, &(u->addr), u->getPort()); //然后根据url建立一个连接
if (res != emptyC) { //下面则为一个具体发送http报文请求的过程
lastAccess = global::now;
conn->timeout = timeoutPage;
conn->request.addString("GET ");
if (global::proxyAddr != NULL) {
char *tmp = u->getUrl();
conn->request.addString(tmp);
} else {
conn->request.addString(u->getFile());
}
conn->request.addString(" HTTP/1.0\r\nHost: ");
conn->request.addString(u->getHost());
#ifdef COOKIES
if (u->cookie != NULL) {
conn->request.addString("\r\nCookie: ");
conn->request.addString(u->cookie);
}
#endif // COOKIES
conn->request.addString(global::headers);
conn->parser = new html (u, conn); //建立页面解析类
conn->pos = 0; //获取的数据中的下标位置 ,pos
conn->err = success;
conn->state = res;
if (tab.isEmpty()) {
isInFifo = false;
} else {
global::okSites->put(this);
}
return 0;
}
下面附带上一次http请求的头部的格式
GET /index.html HTTP/1.1
Accept:text/html
User-Agent:larbin
Host: ioa.rising.com.cn
From:jiangzy@rising.com.cn
相关文章推荐
- larbin源码分析--IPSite
- TcpFlow(1.4.6 C++版本)源代码分析之以太网数据包处理,ip数据包处理
- linxu kernel version 1.0 TCP/IP 协议栈源代码分析2, TCP/IP协议栈启动分析 (按源代码中运行的顺序分析,Kernel startup)
- linxu kernel version 1.0 TCP/IP 协议栈源代码分析1, UDP协议发送数据.
- 如何禁止同IP站点查询和同IP站点查询的原理分析 Robots.txt屏蔽BINGBOT
- LARBIN源代码分析[12]GLOBAL文件中的四个INIT初始化模块函数
- Larbin[1]hashtable checker 源代码分析
- site:pv uv ip
- x264源代码分析
- handler 源代码分析
- s3c2410的Bootloader(Vivi)源代码分析
- Apple公司Darwin流式服务器源代码分析(一)
- Django源代码阅读分析-2:持久层结构分析
- Android应用程序框架层和系统运行库层日志系统源代码分析
- 如何看懂源代码--(分析源代码方法)二
- Hadoop-1.2.1学习之Job创建和提交源代码分析
- FFmpeg源代码简单分析:libswscale的sws_scale()
- BT源代码学习心得(十二):客户端源代码分析(从开始到连接建立阶段)
- hadoop IPC 源代码分析
- Android源码解析之Zygote启动过程的源代码分析