python分布式爬蟲實踐

來源:互聯網
上載者:User

標籤:格式   multi   import   style   實戰   項目實戰   inux   定義   col   

近期閱讀了範傳輝的Python爬蟲開發與項目實戰一書,對其第七章的分布式爬蟲進行實操

遇到自己環境上代碼跑不通的問題,經一番功夫,發現主要是由以下幾點造成的:

書中環境:python2.7,linux

本地環境:python3.4,window

1.文中匯入模組為import Queue,py3中應該為from multiprocessing import Queue

2.window環境下lambda無法被序列化,要使用自訂函數,該自訂函數需要放在代碼開頭,如下;

url_q=Queue()result_q=Queue()def get_url_q():    global url_q    return url_qdef get_result_q():    global result_q    return result_qclass NodeManager(object):    def start_manager(self,url_q,result_q):        BaseManager.register(‘get_task_queue‘,callable=get_url_q)        BaseManager.register(‘get_result_queue‘,callable=get_result_q)

3.window環境下BaseManager函數address傳參為‘’的話不代表本機,linux才代表本機,應明確寫清本機地址,且py3下authkey傳參應該編碼轉換,如下:

manager=BaseManager(address=(‘127.0.0.1‘,8001),authkey=‘baike‘.encode(‘utf-8‘))

4.另外實操中主要遇到的問題就是資料的編碼格式的轉換,編碼格式不同會導致爬蟲執行到某個步驟就一直暫停了(PS:我的是卡在解析網址後一直沒動)

5.代碼主要由兩個視窗執行,其中NodeManager只用於啟動進程,不輸出,SpiderWork進程用於輸出

python分布式爬蟲實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.