代码如下:
1 #!/usr/bin/env python2 2 # vim:fileencoding=utf-8 3 from __future__ import unicode_literals, division, absolute_import, print_function 4 from calibre.web.feeds.news import BasicNewsRecipe 5 6 class AdvancedUserRecipe1547983886(BasicNewsRecipe): # 继承 BasicNewsRecipe 类的新类名 7 #/ 8 # 设置电子书元数据 9 #/ 10 title = 'Runoob Python 2 教程' # 电子书名 11 description = 'Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。本教程主要针对Python 2.x版本的学习,如果你使用的是Python 3.x版本请移步至Python 3.X版本的教程。' # 电子书简介 12 cover_url = 'http://www.runoob.com/wp-content/uploads/2013/11/python.jpg' # 电子书封面 13 # masthead_url = '' # 页头图片 14 __author__ = 'Runoob' # 作者 15 language = 'zh' # 语言 16 encoding = 'utf-8' # 编码 17 #/ 18 # 抓取页面内容设置 19 #/ 20 #keep_only_tags = [{ 'class': 'example' }] # 仅保留指定选择器包含的内容 21 no_stylesheets = True # 去除 CSS 样式 22 remove_javascript = True # 去除 JavaScript 脚本 23 # oldest_article = 7 24 max_articles_per_feed = 999 # 抓取文章数量 25 delay = 5 # 抓取资源间隔时间 26 auto_cleanup = True 27 #/ 28 # 页面内容解析方法 29 #/ 30 def parse_index(self): 31 site = 'http://www.runoob.com/python/python-tutorial.html' # 页面列表页 32 soup = self.index_to_soup(site) # 解析列表返回 BeautifulSoup 对象 33 links = soup.findAll('a', {'target': '_top'}) # 获取所有文章的链接地址 34 articles = [] # 定义文章资源数组 35 for link in links: # 循环处理所有文章链接 36 title = link.contents[0].strip() # 提取文章标题 37 print(title) 38 print(link) 39 url = site + link['href'] # 提取文章链接 40 print(u'%s'%(url)) 41 a ={'title':title,'url':url} # 组合标题和链接 42 articles.append(a) # 累加到数组中 43 ans = [(self.title,articles)] # 组成最终的数据结构 44 return ans # 返回可供 Calibre 转换的数据结构