步骤 1 - 样式识别

首先我们需要识别HTML代码部分,这些代码包含了我们的信息。您能从图片上看到我们想采集的的信息(红箭头标注)。

在页面源代码处,主要的文章已经被高亮显示:

<div style="background-color: #EAEFF4;"> <div class="cnnMainT1Hd"><h2><a href="/2004/ALLPOLITICS/12/20/bush.ap/index.html" style="color:#000;"> Bush looks to slash deficit</a></h2></div><div style="background-color:#fff;"><img src="http://i.cnn.net/cnn/img/1.gif" alt="" width="1" height="10"></div> <!-- REAP --><a href="/2004/ALLPOLITICS/12/20/bush.ap/index.html"><img src="http://i.a.cnn.net/cnn/2004/ALLPOLITICS/12/20/bush.ap/top.bush.flag.ap.jpg"width="280" height="210" alt="Bush looks to slash deficit" border="0" hspace="0" vspace="0"></a><!-- PURGE: /2004/ALLPOLITICS/12/20/bush.ap/top.bush.flag.ap.jpg --><!-- KEEP --><!-- /REAP --><div class="cnnMainT1"><p> President Bush today covered a range of issues at a news conference in Washington. Bush said:<li> He would submit a budget that cuts the deficit in half and maintains strict spending discipline </li><li> Iraqi elections in January "are the beginning of a process, and it is important for the American people to understand that" </li><li> Defense Secretary Donald Rumsfeld is "doing a very fine job" </li></p><p><a href="/2004/ALLPOLITICS/12/20/bush.ap/index.html" class="cnnt1link">FULL STORY</a></p><p> <b><span class="cnnBodyText" style="font-weight:bold;color:#333;">Video: </span><img src="http://i.cnn.net/cnn/.element/img/1.0/misc/premium.gif" alt="premium content" width="9" height="11" hspace="0" vspace="0" border="0" align="absmiddle"></b> <a href="javascript:LaunchVideo('/politics/2004/12/20/sot.bush.domestic.agenda.cnn.','300k');">Bush sets out economic agenda</a><br> <b><span class="cnnBodyText" style="font-weight:bold;">CNN/Money: </span></b> <a href="/money/2004/12/20/retirement/bush_pressconf/index.htm">Social security questions</a><br> <b>Transcript:</b> <a href="/2004/ALLPOLITICS/12/20/bush.transcript.ap/index.html">Bush news conference</a><br> <b><span class="cnnBodyText" style="font-weight:bold;color:#333;">Special Report: </span></b> <a href="/SPECIALS/2004/bush.term/">Bush: The Second Term</a><br></p></div></div>

我们只想使用主要文章的标题和图片下方的短语。
正如我们所看到的,标题被Tag封装 <div class="cnnMainT1Hd"></div> 短语也在Tag封装里 <div class="cnnMainT1"></div>

步骤 2 - 脚本的头版

现在我们将开始此脚本。首先我们将定制主要部分,将下载www.cnn.com主页的内容。

#脚本主体
<Section>
    #主体名称定义
    name ourMainSection


    # 加载内容
    <Action ContentURL>

        #从下面的网址加载内容
        URL http://www.cnn.com
        
        #为了更易匹配,从内容中移除空行 
        RemoveNewLine
    </Action>

</Section>

#运行 “ourMainSection”
main ourMainSection

每一个脚本包括从URL下载指定页面的主要部分,然后执行某些行为,随着下载的内容。我们将在下一步看到如何匹配数据。

步骤 3 - 匹配标题

在步骤2里,我们从网站上加载了内容,因此现在我们能试着匹配只要文章的标题和进行默认打印。

#脚本主体
<Section>

    #主体命名
    Name ourMainSection

    # 加载内容
    <Action ContentURL>
        #从下面网址加载内容
        URL http://www.cnn.com
        
        #为了更易匹配,从下载的内容中移除空行
        RemoveNewLine
    </Action>


    #此样式应该匹配标题
    <Pattern>

	#defines expression which should match the data
       RegExp <div class="cnnMainT1Hd"><h2><a*>\
       	  {$main_title}</a></h2></div>

    </Pattern>

    #打印输出数据
    <Action Print>
        Text Story of the day: {$main_title}\n
    </Action>

</Section>

#运行 “ourMainSection”
Main ourMainSection

为了匹配数据,我们必须指定匹配样式。下面的样式告诉系统去匹配在标签间的所有内容,<div class="cnnMainT1Hd"><h2><a*>...</a></h2></div>,并且存储匹配的值到变量$main_title中。

然后我们定义一个行为,此行为可以针对这个变量做一些工作。在我们的案例中,它只是使用Text命令标准打印输出结果。但是您可以保存值到文件,做一个数据库插入,或者使用此值加载或采集另外一个页面。

步骤 4 - 匹配图片下的文字

作为最后一步,我们需要增加一个Pattern标签,能够匹配主要文章的短语。

#脚本主体
<Section>
    #定义主体名称
    Name ourMainSection

    # 加在内容
    <Action ContentURL>
        #从下面的链接加在内容
        URL http://www.cnn.com
        
        #为了更易匹配,从加载的内容中移除空行 
        RemoveNewLine
    </Action>

    #此样式应该匹配标题
    <Pattern>

	#定义匹配数据的表达
       RegExp <div class="cnnMainT1Hd"><h2><a*>\
       	  {$main_title}</a></h2></div>
    </Pattern>

    #打印匹配的数据到默认输出
    <Action Print>

        Text Story of the day: {$main_title}\n
    </Action>

    #从主题文章中匹配样式
    <Pattern>
       RegExp <div class="cnnMainT1">\
       {$short_text:re(.*?)}</div>
    </Pattern>

    #打印匹配的短语
    <Action Print>
        Text Text: {$short_text}\n
    </Action>

</Section>

#运行 “ourMainSection”
Main ourMainSection

在此,我们定义超过一个样式,它将匹配在<div class="cnnMainT1">...</div>间的内容,并且存储匹配的值到变量$short_text

注意:我们在此变量后,使用特殊的修改器:re(.*?)

:re 的意思是告诉系统使用标准的表达时区匹配文字。

.*? 是匹配每个字符的标准表达式,知道字符‘<’。

我们同时定义超过一个行为,标准打印输出匹配的文字。

© 2004-2012 QualityUnit.com, All rights reserved