搜索引擎——原理技术与系统第三章

        从本章开始的连续三章,我们讲解一个完整的搜索引擎TSE(Tiny Search Engine)的实现,编程语言采用C++,代码可以在[TSE,2004]下载。TSE包括三段式工作流程,分别对应本章的Web信息的搜集,第四章搜集信息的预处理和第五章的信息查询服务。

超文本传输协议(Hypertext Transfer Protocol, HTTP)1是Web的基础协议。为了本章的完整,首先对HTTP进行简要的介绍,然后重点讲解如何实现Web信息的收集。

HTTP是一个简单的协议。客户进程建立一条同服务器进程的TCP连接,然后发出请求并读取服务器进程的应答。服务器进程关闭连接表示本次响应结束。服务器进程返回的内容包含两个部分,一个“应答头”(response header),一个“应答体”(response body),后者通常是一个HTML文件,我们称之为“网页”。

网页搜集子系统,就是第一章第二节和第二章第五节中讲到的spider,可以用C/C++、Perl、Java,Python等语言来编写,可以运行在Intel, Sparc, Mac等平台上的Unix或Window系统下。网页“爬取器”(gatherer),指网页搜集子系统中根据URL完成一篇网页抓取的进程或者线程,通常一个spider会同时启动多个gatherer并行工作。Spider设计是否合理将直接影响它访问Web的效率,影响搜集数据的质量,另外,在设计spider时还必须考虑它对网络和被访问站点的影响,因为spider一般都运行在速度快、带宽高的主机上,如果它快速访问一个速度比较慢的目标站点,就有可能会导致该站点出现拥塞甚至宕机。Spider还应遵守一些协议(例如:robot限制协议[Wong,1997]),尊重被访问站点管理员确定的内容保护策略。

图3-4所示为TSE系统结构,对应于搜索引擎三段式工作流程,是图中左侧的A表示搜集部分,中间的B表示整理(即预处理)部分和右侧的C表示服务部分。其中黄色圆柱形图表示数据产品,按照统一并且简单易懂的格式存储,除本系统使用外,可以提供给其他科研机构使用;椭圆形绿色图表示系统流程中的内部数据,由于与系统中使用的数据结构结合紧密,不适合作为数据产品提供给其他研究机构;矩形蓝色表示系统流程的程序部分(过程),是数据产品与内部数据之间的桥梁。系统起始于A搜集,结束于C服务,整个流程可以重复进行,从而达到系统的更新。

本章后续内容讲解搜集部分(对应图3-4的A) ;第四章讲解预处理部分(对应图中的B);第五章讲解服务部分(对应图中的C)。

         


在TSE中网页搜集对应图3-4的左侧部分,我们将它细化为图3-5所示。网页搜集的过程是从URL库(初始时包含用户指定的起始种子URL集合,可以是

1个或多个)获得输入,解析URL中标明的Web服务器地址、建立连接、发送请求和接收数据,将获得的网页数据存储在原始网页库,并从其中提取出链接信息放入网页结构库,同时将待抓取的URL放入URL库,保证整个过程的递归进行,直到URL库为空。


搜索引擎为了提供检索服务,需要保存网页原文。网页搜集子系统不但要能够获取以.html, .htm, .txt结尾的URL对应的网页(在本章后面的小节对于搜集信息类型会有更详细的阐述),还应该能够获取不是以.html结尾的URL,比如.pdf,.doc,因为.pdf,.doc等文件可以通过转换程序生成为.html或者.txt文件,同样为搜索引擎提供检索服务。作为搜索引擎的起始流程,搜集的网页要按照一定的格式存储,便于后续组织和提供服务。



定义URL类和Page类

下面是URL类的定义,对应文件Url.h。
enum url_scheme{
SCHEME_HTTP,
SCHEME_FTP,
SCHEME_INVALID
};
class CUrl
{
public:
string m_sUrl; // URL字串
enum url_scheme m_eScheme; // 协议名
string m_sHost; // 主机名
int m_nPort; // 端口号
string m_sPath; // 请求资源
public:
CUrl();
~CUrl();
bool ParseUrl( string strUrl );
private:
void ParseScheme ( const char *url );
};

URL可以是HTTP,FTP等协议开始的字符串,TSE主要是针对HTTP协议,为了不失一般性,在url_scheme中定义了SCHEME_HTTP,SCHEME_FTP,SCHEME_INVALID,分别对应HTTP协议,FTP协议和其他协议。一个URL由6个部分组成:
<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>
除了scheme部分,其他部分可以不在URL中同时出现。
Scheme 表示协议名称,对应于URL类中的m_eScheme.
Net_loc 表示网络位置,包括主机名和端口号,对应于URL类中的m_sHost和m_nPort.
下面四个部分对应于URL类中的m_sPath.

Path 表示URL 路径.
Params 表示对象参数.
Query 表示查询信息,也经常记为request.
Fragment 表示片断标识.






TSE(Tiny Search Engine) ======================= (Temporary) Web home: http://162.105.80.44/~yhf/Realcourse/ TSE is free utility for non-interactive download of files from the Web. It supports HTTP. According to query word or url, it retrieve results from crawled pages. It can follow links in HTML pages and create output files in Tianwang (http://e.pku.edu.cn/) format or ISAM format files. Additionally, it provies link structures which can be used to rebuild the web frame. --------------------------- Main functions in the TSE: 1) normal crawling, named SE, e.g: crawling all pages in PKU scope. and retrieve results from crawled pages according to query word or url, 2) crawling images and corresponding pages, named ImgSE. --------------------------- INSTALL: 1) execute "tar xvfz tse.XXX.gz" --------------------------- Before running the program, note Note: The program is default for normal crawling (SE). For ImgSE, you should: 1. change codes with the following requirements, 1) In "Page.cpp" file, find two same functions "CPage::IsFilterLink(string plink)" One is for ImgSE whose urls must include "tupian", "photo", "ttjstk", etc. the other is for normal crawling. For ImgSE, remember to comment the paragraph and choose right "CPage::IsFilterLink(string plink)". For SE, remember to open the paragraph and choose righ "CPage::IsFilterLink(string plink)". 2) In Http.cpp file i. find "if( iPage.m_sContentType.find("image") != string::npos )" Comment the right paragraph. 3) In Crawl.cpp file, i. "if( iPage.m_sContentType != "text/html" Comment the right paragraph. ii. find "if(file_length < 40)" Choose right one line. iii. find "iMD5.GenerateMD5( (unsigned char*)iPage.m_sContent.c_str(), iPage.m_sContent.length() )" Comment the right paragraph. iv. find "if (iUrl.IsImageUrl(strUrl))" Comment the right paragraph. 2.sh Clean; (Note not remove link4History.url, you should commnet "rm -f link4History.url" line first) secondly use "link4History.url" as a seed file. "link4History" is produced while normal crawling (SE). --------------------------- EXECUTION: execute "make clean; sh Clean;make". 1) for normal crawling and retrieving ./Tse -c tse_seed.img According to query word or url, retrieve results from crawled pages ./Tse -s 2) for ImgSE ./Tse -c tse_seed.img After moving Tianwang.raw.* data to secure place, execute ./Tse -c link4History.url --------------------------- Detail functions: 1) suporting multithreads crawling pages 2) persistent HTTP connection 3) DNS cache 4) IP block 5) filter unreachable hosts 6) parsing hyperlinks from crawled pages 7) recursively crawling pages h) Outputing Tianwang format or ISAM format files --------------------------- Files in the package Tse --- Tse execute file tse_unreachHost.list --- unreachable hosts according to PKU IP block tse_seed.pku --- PKU seeds tse_ipblock --- PKU IP block ... Directories in the package hlink,include,lib,stack,uri directories --- Parse links from a page --------------------------- Please report bugs in TSE to MAINTAINERS: YAN Hongfei * Created: YAN Hongfei, Network lab of Peking University. * Created: July 15 2003. version 0.1.1 * # Can crawl web pages with a process * Updated: Aug 20 2003. version 1.0.0 !!!! * # Can crawl web pages with multithreads * Updated: Nov 08 2003. version 1.0.1 * # more classes in the codes * Updated: Nov 16 2003. version 1.1.0 * # integrate a new version linkparser provided by XIE Han * # according to all MD5 values of pages content, * for all the pages not seen before, store a new page * Updated: Nov 21 2003. version 1.1.1 * # record all duplicate urls in terms of content MD5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值