Prvý krok - identifikácia dát

Najprv musíme identifikovať tie časti HTML kódu, ktoré obsahujú želanú informáciu. Na obrázku môžete vidieť, ktoré informácie chceme extrahovať (sú označené červenou šípkou).

Hlavný článok je vyznačený v zdrojovom kóde:

<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>

Chceli by sme extrahovať iba titulok článku a krátky text pod obrázkom. Ako vidíme, titulok je ohraničený tagmi <div class="cnnMainT1Hd"></div> a krátky text je medzi <div class="cnnMainT1"></div>

Druhý krok - prvá verzia skriptu

Teraz začneme písať skript. Ako prvé si zadefinujeme hlavnú sekciu, ktorá bude sťahovať obsah stránky cnn.com .

#main section of script
<Section>
    #define name of section
    Name ourMainSection


    # Load content
    <Action ContentURL>

        #load content from the following URL
        URL http://www.cnn.com
        
        #removes newlines from downloaded content for easier matching 
        RemoveNewLine
    </Action>

</Section>

#run section with name “ourMainSection”
Main ourMainSection

Každý skript obsahuje hlavnú sekciu, ktorá stiahne stránku z požadovanej URL a potom niečo spraví s jej obsahom. V nasledujúcom kroku sa pozrieme ako zachytiť dáta.

Tretí krok - vytiahnutie titulku

V druhom kroku sme stiahli obsah z webstránky, teraz sa pokúsime vytiahnuť titulok a vypísať ho.

#main section of script
<Section>

    #define name of section
    Name ourMainSection

    # Load content
    <Action ContentURL>
        #load content from the following URL
        URL http://www.cnn.com
        
        #removes newlines from downloaded content for easier matching 
        RemoveNewLine
    </Action>


    #this pattern should match main story title
    <Pattern>

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

    </Pattern>

    #print matched data to default output
    <Action Print>
        Text Story of the day: {$main_title}\n
    </Action>

</Section>

#run section with name “ourMainSection”
Main ourMainSection

Aby sme správne vytiahli dáta, musíme špecifikovať porovnávajúci výraz. Nasledujúci výraz povie systému, aby vytiahol všetko medzi tagmi <div class="cnnMainT1Hd"><h2><a*>...</a></h2></div> a uložil vytiahnutý obsah do premennej $main_title

Potom si definujeme, čo spravíme s hodnotou premennej. V našom prípade ju len vypíšeme na štandardný výstup pomocou príkazu Text, no môžete si ju uložiť do súboru, spraviť insert do databázy alebo použiť túto premennú na stiahnutie ďalšej stránky.

Štvrtý krok - vytiahnutie textu pod obrázkom

V poslednom kroku pridáme ešte jeden Pattern tag, ktorý vytiahne krátky text z hlavného článku.

#main section of script
<Section>
    #define name of section
    Name ourMainSection

    # Load content
    <Action ContentURL>
        #load content from the following URL
        URL http://www.cnn.com
        
        #removes newlines from downloaded content for easier matching 
        RemoveNewLine
    </Action>

    #this pattern should match main story title
    <Pattern>

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

    #print matched data to default output
    <Action Print>

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

    #match short text from main story
    <Pattern>
       RegExp <div class="cnnMainT1">\
       {$short_text:re(.*?)}</div>
    </Pattern>

    #print matched short text
    <Action Print>
        Text Text: {$short_text}\n
    </Action>

</Section>

#run section with name “ourMainSection”
Main ourMainSection

Tu sme si zadefinovali ešte jeden výraz, ktorý vytiahne všetko medzi tagmi <div class="cnnMainT1">...</div> a uloží hodnotu do premennej $short_text.

Všimnite si, že sme použili špeciálny výraz :re(.*?) po premennej.

:re hovorí systému, že použijeme regulérny výraz na vytiahnutie textu

.*? je regulérny výraz, ktorý namapuje všetky znaky po znak ‘<’.

Tiež sme zadefinovali ešte jednu Action, ktorá vypíše vytiahnutý text na štandardný výstup.

© 2004-2012 QualityUnit.com, All rights reserved