試用Web-Harvest

來源:互聯網
上載者:User

                  試用Web-Harvest
        Web-Harvest是一個Java開源Web資料幫浦工具。它能夠收集指定的Web頁面並從這些頁面中提取有
用的資料。Web-Harvest主要是運用了像XSLT,XQuery,Regex等這些技術來實現對text/xml的操作。
       個人感覺,這個工具的設計構想很好,利用寫好的xml指令碼把指定的html轉化成xml,然後再利用xml
解析器從中抽取資訊。這樣在編寫網頁資訊抽取工具時,我們就不用擔心網頁格式的變化,會影響到資訊
抽取的結果,因為整個抽取資訊的部分都是通過配置對應的指令碼實現的,我們只要修改指令碼就可以了,不
用更改程式碼。
 下面是我按照這個開源工具自己帶的例子改寫的一個提取yahoo搜尋資訊的指令碼,用Web-Harvest執行它
,可以提取以"KMS"為關鍵字在yahoo搜尋引擎上搜尋得到的結果。

指令碼xml:
<?xml version="1.0" encoding="UTF-8"?>
<config charset="ISO-8859-1">
   
    <include path="functions.xml"/>
   
    <var-def name="search">KMS</var-def>
 <var-def name="url">
     <template>http://search.yahoo.com/search?p=${search}</template>
 </var-def>
           
    <!-- collects all tables for individual products -->
    <var-def name="products">   
        <call name="download-multipage-list">
            <call-param name="pageUrl"><var name="url"/></call-param>
            <call-param name="nextXPath">//big[.='Next']/a/@href</call-param>
            <call-param name="itemXPath">//ol/li</call-param>
            <call-param name="maxloops">10</call-param>
        </call>
    </var-def>
   
    <!-- iterates over all collected products and extract desired data -->
    <file action="write" path="myyahoo.xml" charset="UTF-8">
        <![CDATA[ yahoo ]]>
        <loop item="item" index="i">
            <list><var name="products"/></list>
            <body>
                <xquery>
                    <xq-param name="item"><var name="item"/></xq-param>
                    <xq-expression><![CDATA[
                            let $name := data($item//div[1]/a[1])
                            let $src := data($item//div[1]/a[1]/@href)
                            let $abs := data($item//div[2])
                                return
                                    <product>
                                        <name>{normalize-space($name)}</name>
                                        <src>{normalize-space($src)}</src>
                                        <abs>{normalize-space($abs)}</abs>
                                    </product>
                    ]]></xq-expression>
                </xquery>
            </body>
        </loop>
        <![CDATA[ yahoo ]]>
    </file>

</config>

結果xml:
<yahoo><product>
   <name>KMSResearch</name>
   <src>http://rds.yahoo.com/_ylt=A0geuodL05lFpaEArQxXNyoA;_ylu=X3oDMTB2b2gzdDdtBGNvb
G8DZQRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=11fph2etm/EXP=1167795403/**http%3a//www.kmshaircare.com/</src>
   <abs>Learn about each subbrand which has its own purpose and look to support your way of
life, mood, or whim.</abs>
</product>
.
.
.
<product>
   <name>Summer - KMS promotional items</name>
   <src>http://rds.yahoo.com/_ylt=A0geupZ705lFwVkAMQZXNyoA;_ylu=X3oDMTExYm1vY2p0BGNvb
G8DZQRsA1dTMQRwb3MDMTAwBHNlYwNzcgR2dGlkAw--/SIG=11q4tb45p/EXP=1167795451/**http%3a//kms-fra.com/en/products/sommer/</src>
   <abs>KMS Design. Special designs. Onpacks and Inpacks ... KMS presents the smallest solar
charger available. ... The KMS SoftFrisbee - this UFO is foldable! ...</abs>
</product></yahoo>

        如果你對xml,xpath,xquery這些技術都有瞭解,在看過Web-Harvest的協助(http://web-harvest.sourceforge.net/manual.php)後,相信上面的指令碼xml應該不難理解。
       在整個試用過程中,我也發現了Web-Harvest的一些問題,比如他使用tagsoup對html網頁進行清洗
,會造成一些格式不太規範的網頁資料丟失(比如google的搜尋網頁面),希望Web-Harvest的開發人員能夠
注意到這個問題,畢竟現在能夠嚴格遵守HTML4.0規範的網頁並不多,更多的是xml出現之前就已經存在
的網頁。現在的web資訊抽取使用xml技術實現無疑是最理想的,而Web-Harvest已經為我們搭建了一個
可供選擇的抽模數型,如何解決對大量不規範網頁的無損xml轉換,將是這個工具能否運用到實際中的關
鍵環節。
        還有,由於本人水平有限,在利用Web-Harvest抽取中文的網頁,還沒發現完全沒有亂碼的網頁。
        這篇文章的目的是希望能夠拋磚引玉,能有更多的人關注Web-Harvest這個工具。因為Web-Harvest
還有很多進階的應用我還沒有研究;還有很多需要改進的地方。但它至少給我一個啟示,完全結構化的,
動態化的網頁資訊抽取是可以實現的,而且並不難。
參考文獻:
Web-Harvest:http://web-harvest.sourceforge.net/
XPath教程:http://www.zvon.org/xxl/XPathTutorial/Output_chi/introduction.html
XQuery教程:http://www.w3pop.com/tech/school/xquery/default.asp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.