用calibre和python制作电子书---Python和Git教程

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

原文地址:https://www.cnblogs.com/ckzhou/p/4162835.html