calibre - E-book management是一個很強大的電子書管理軟件,可以打開和轉換各種格式的電子書,可以抓取新聞到本地閱讀,允許用戶自定義新聞源,可以通過編寫自己recipe讓其將網頁上的內容抓取下來並且生成電子書,利用這個功能我將廖雪峰老師的Python教程和Git教程做成了epub電子書,使用firefox的epubReader插件就可以在電腦上打開閱讀了,手機在多看上閱讀也可以,這體驗對於假期回家沒有網絡但是又想學習的同學們來說還是不錯的。
recipe采用python編寫,用學到的python去獲取學習資源,加強實踐,操作方式很簡單,下載calibre - E-book management后就知道了,calibre也為recipe的編寫提供了api文檔
下面是抓取python教程的recipe代碼
#!/usr/bin/env python # vim:fileencoding=utf-8 from __future__ import unicode_literals, division, absolute_import, print_function from calibre.web.feeds.news import BasicNewsRecipe class liaoxuefeng_python(BasicNewsRecipe): title = '廖雪峰Python教程' description = 'python教程' max_articles_per_feed = 200 url_prefix = 'http://www.liaoxuefeng.com' no_stylesheets = True keep_only_tags = [{ 'id': 'main' }] remove_tags=[{'class':'x-wiki-info'}] remove_tags_after=[{'class':'x-wiki-content x-content'}] def get_title(self, link): return link.contents[0].strip() def parse_index(self): soup = self.index_to_soup('http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000') div = soup.find('div', { 'class': 'x-wiki-tree' }) articles = [] for link in div.findAll('a'): til = self.get_title(link) url = self.url_prefix + link['href'] a = { 'title': til, 'url': url } articles.append(a) tutorial = [('廖雪峰python教程', articles)] return tutorial
抓取Git教程只需要將parse_index方法中python教程的鏈接改為Git教程的鏈接就可以了,成品在這tutorial
