python 用於網站抓取 登錄 發佈的模塊介紹

由於目前的Web開發中AJAX、Javascript、CSS的大量使用,一些網站上的重要數據是由Ajax或Javascript動態生成的,並不能直接通過解析html頁面內容就能獲得(例如採用mechanizelxmlBeautiful Soup )。要實現對這些頁面數據的爬取,爬蟲必須支持Javacript、DOM、HTML解析等一些瀏覽器html、javascript引擎的基本功能。

正如Web Browser Programming in Python總結的,在python程序中,有如下一些項目提供能類似功能:

其中Pyv8主要是Google Chrome V8 Javascript引擎的Python封裝,側重在Javacript操作上,並不是完整的Web Browser 引擎,而諸如PythonWebKitPython-SpidermonkeyPyWebKitGtk等幾個主要在Linux平臺上比較方便,而HulaHopPamie處理MS IE 。因此從跨平臺、跨瀏覽器、易用性等角度考慮,以上方案並不是最好的。

SeleniumWindmill 原本主要用於Web自動化測試上,對跨操作系統、跨瀏覽器有較好的支持。其對Javacript、DOM等操作的支持主要依賴操作系統本地的瀏覽器引擎來實現,因此爬蟲所必須的大部分功能,SeleniumWindmill 都有較好的支持。在性能要求不高的情況下,可以考慮採用SeleniumWindmill的方案,從評價來看,WindmillSelenium功能更加全面。

爭取其他語言一些類似的軟件還有:

  • Lobo Browser(Java Browser)
  • Rhino (Java Javascript Engine)
  • HtmlunitTestNG (Java  Testing Framework)
  • Watir (Ruby)
  • 1、應用場景

    關於Selenium的詳細說明,可以參考其文檔, 這裏使用Python+Selenium Remote Control (RC)+Firefox 來實現如下幾個典型的功能:

    1)、Screen Scraping,也即由程序自動將訪問網頁在瀏覽器內顯示的圖像保存爲圖片,類似那些digg站點的網頁縮略圖。Screen Scraping有分成兩種:只Scraping當前瀏覽器頁面可視區域網頁的圖片(例如google.com首頁),Scraping當前瀏覽器完整頁面的圖片(頁面有滾動,例如www.sina.com.cn的首頁有多屏,需要完整保存下來)

    2)、獲取Javascript腳本生成的內容

    例如要用程序自動爬取並下載百度新歌TOP100 的所有新歌,以下載蕭亞軒的《抱緊你》爲例,大致步驟可以如下:

    a)、進入百度新歌TOP100http://list.mp3.baidu.com/top/top100.html,通過正則表達式匹配<a target=”_blank” href=”http://mp3.baidu.com/m?(.*)” class=”search”></a> 或採用mechanizeBeautiful Soup之類的htmlparser解析頁面獲得每一首歌后面的查詢地址

    b)、在查詢結果頁面,獲得第一條結果的地址<a href=”http://202.108.23.172(.*)” title=”(.*)</a>,進入mp3的實際下載地址

    c)、在歌曲實際下載頁面,解析html頁面內容,會發現mp3的實際現在地址爲空

       <a id="urla" href="" οnmοusedοwn="sd(event,0)" target="_blank"></a>
    

    實際的下載地址是由javascript腳本設置的:

                        var encurl = "…", newurl = "";
                        var urln_obj = G("urln"), urla_obj = G("urla");
                        newurl = decode(encurl);
                        urln_obj.href = urla_obj.href = song_1287289709 = newurl;
      其中函數G(str)爲:
    
               function G(str){
                            return document.getElementById(str);
                    };
    

    因此直接解析頁面並不能獲得下載地址,必須通過python調用瀏覽器引擎來解析javascript代碼後獲得對應的下載地址。

    2、Selenium RC基礎

    Selenium RC的運行機制及架構在官方文檔中有詳細說明

    Selenium RC主要包括兩部分:Selenium Server、Client Libraries,其中:

    • The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT.

    Selenium Server 對應Selenium RC 開發包中的selenium-server-xx目錄,其中

    xx對應相應的版本

    • Client libraries which provide the interface between each programming language and the Selenium-RC Server.

    Selenium RC提供了包括java、python、ruby、perl、.net、php等語言的client driver,分別如下:

    selenium-dotnet-client-driver-xx

    selenium-java-client-driver-xx

    selenium-perl-client-driver-xx

    selenium-php-client-driver-xx

    selenium-python-client-driver-xx

    selenium-ruby-client-driver-xx

     

    Python等語言通過調用client driver來發出瀏覽器操作指令(例如打開制定url),由client driver把指令傳遞給Selenium Server解析。Selenium Server負責接收、解析、執行客戶端執行的Selenium 指令,轉換成各種瀏覽器的命令,然後調用相應的瀏覽器API來完成實際的瀏覽器操作。

    Selenium Server實際充當了客戶端程序與瀏覽器間http proxy。

    3、例子:

    1)、下載Selenium RC http://seleniumhq.org/download/,測試使用的selenium-remote-control-1.0.3.zip

    2)、解壓後selenium-remote-control-1.0.3.zip

    3)、運行Selenium Server

    cd  selenium-remote-control-1.0.3\selenium-server-1.0.3

    java -jar  selenium-server.jar

    Selenium Server缺省監聽端口爲4444,在org.openqa.selenium.server.RemoteControlConfiguration中設定

    4)、測試代碼

    #coding=gbk
    from selenium import selenium
    
    def selenium_init(browser,url,para):
        sel = selenium('localhost', 4444, browser, url)
        sel.start()
        sel.open(para)
        sel.set_timeout(60000)
        sel.window_focus()
        sel.window_maximize()
        return sel     
    
    def selenium_capture_screenshot(sel):
        sel.capture_screenshot("d:\\singlescreen.png")
    
    def selenium_get_value(sel):
        innertext=sel.get_eval("this.browserbot.getCurrentWindow().document.getElementById('urla').innerHTML")
        url=sel.get_eval("this.browserbot.getCurrentWindow().document.getElementById('urla').href")
        print("The innerHTML is :"+innertext+"\n")
        print("The url is :"+url+"\n")
    
    def selenium_capture_entire_page_screenshot(sel):
        sel.capture_entire_page_screenshot("d:\\entirepage.png", "background=#CCFFDD")
    
    if __name__ =="__main__" :
        sel1=selenium_init('*firefox3','http://202.108.23.172','/m?word=mp3,http://www.slyizu.com/mymusic/VnV5WXtqXHxiV3ZrWnpnXXdrWHhrW3h9VnRkWXZtXHp1V3loWnlrXXZlMw$$.mp3,,[%B1%A7%BD%F4%C4%E3+%CF%F4%D1%C7%D0%F9]&ct=134217728&tn=baidusg,%B1%A7%BD%F4%C4%E3%20%20&si=%B1%A7%BD%F4%C4%E3;;%CF%F4%D1%C7%D0%F9;;0;;0&lm=16777216&sgid=1')
        selenium_get_value(sel1)
        selenium_capture_screenshot(sel1)
        sel1.stop()
        sel2=selenium_init('*firefox3','http://www.sina.com.cn','/')
        selenium_capture_entire_page_screenshot(sel2)
        sel2.stop()
    

    幾點注意事項:

    1)、在selenium-remote-control-1.0.3/selenium-python-client-driver-1.0.1/doc/selenium.selenium-class.html 中對Selenium支持的各種命令的說明,值得花點時間看看

    2)、在__init__(self, host, port, browserStartCommand, browserURL) 中,browserStartCommand爲使用的瀏覽器,目前Selenium支持的瀏覽器對應參數如下:
    *firefox
    *mock
    *firefoxproxy
    *pifirefox
    *chrome
    *iexploreproxy
    *iexplore
    *firefox3
    *safariproxy
    *googlechrome
    *konqueror
    *firefox2
    *safari
    *piiexplore
    *firefoxchrome
    *opera
    *iehta
    *custom

    3)、capture_entire_page_screenshot目前只支持firefox、IE

    使用firefox時候使用capture_entire_page_screenshot比較簡單,不需要特別設置,Selenium會自動處理。因此如果使用capture_entire_page_screenshot推薦使用firefox。

    IE必須運行在非HTA(non-HTA)模式下(browserStartCommand值爲:*iexploreproxy ),並且需要安裝http://snapsie.sourceforge.net/ 工具包,具體可以參考這篇文章:Using captureEntirePageScreenshot with Selenium

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章