当前位置:首页 > 技术手札 > 正文内容

carlibre抓取网页内容生成电子书

秋风渡红尘8年前 (2018-06-27)技术手札3025
#!/usr/bin/python
# encoding: utf-8
from calibre.web.feeds.recipes import BasicNewsRecipe
class Pro_Git_Chinese(BasicNewsRecipe):

    title = 'QCustomplot'
    description = 'QCustomplot介绍'
    cover_url = ''
    _author_ = '朝十晚八'

    url_pre = 'https://www.cnblogs.com/swarmbees/category/908110.html'
    no_stylesheets = True #去除css
    keep_only_tags = [{ 'class': 'blogpost-body' }] #仅在blogpost-body里面查找
    simultaneous_downloads = 1 #最大下载线程,默认为5

    def parse_index(self):
# recipe的核心method,通过分析目录页,找到各页面链接,并抓取内容,返回一个较复杂的数据结构
        soup = self.index_to_soup(self.url_pre)#目录页
	#查找div,其class属性为entrylist,因为列表是处于<div class="entrylist">容器中
        div = soup.find('div', {'class': 'entrylist'})#目录页的寻找范围

        articles = []
        for link in div.findAll('a', id=True):#循环查找标签a,且其id要为真。
            til = link.contents[0].strip() #获取标题,去除空格
            url = link['href'] #获取标题的链接
            a = { 'title': til, 'url': url }
            articles.insert(0,a) #append(a)是在列表末尾追加,现在改成insert(0,a)在列表前面插入
        results = [(self.title, articles)] #结果由标题和文章组成
        return results

--------------------------------------------------------------------------------------------------------------------


#!/usr/bin/python
# encoding: utf-8
from calibre.web.feeds.recipes import BasicNewsRecipe
class Pro_Git_Chinese(BasicNewsRecipe):

    title = 'QT学习之路2'
    description = ''
    __author__ = '豆子'
    cover_url = ''
    simultaneous_downloads = 5

    url_pre = 'https://www.devbean.net/2012/08/qt-study-road-2-catelog/'
    no_stylesheets = True
    remove_javascript = True
    keep_only_tags = [{ 'class': 'thecontent clearfix' }]

    def parse_index(self):
# recipe的核心method,通过分析目录页,找到各页面链接,并抓取内容,返回一个较复杂的数据结构
        soup = self.index_to_soup(self.url_pre)#目录页

        div = soup.find('ol')#目录页的寻找范围
	
        articles = []
        for link in div.findAll('a'):
            til = link.contents[0].strip()
            url = link['href']
            a = { 'title': til, 'url': url}

            articles.append(a)

        results = [(self.title, articles)]

        return results


扫描二维码推送至手机访问。

版权声明:本文由咿呀贝发布,如需转载请注明出处。

本文链接:https://yiyabei.cn/?id=22

标签: calibre
分享给朋友:

相关文章

QT编译的程序中文乱码

QT编译的程序中文乱码

在主程序加入 QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));...

ubuntu 安装mysql

ubuntu 安装mysql

sudo apt-get install mysql-server sudo apt isntall mysql-client sudo apt install libmysqlclient-dev 检测是否安装成功: sudo n...

BBR+BBR魔改+锐速一键脚本 for Centos/Debian/Ubuntu

BBR+BBR魔改+锐速一键脚本 for Centos/Debian/Ubuntu

此文为引用文。 支持系统:Centos 6+/Debian 8+/Ubuntu 14+,BBR魔改版不支持Debian 8 wget -N --no-check-certificate...

word/wps文档图片只显示一行高度

word/wps文档图片只显示一行高度

word/wps文档插入图片时只显示一行高度,无法完整显示图片。 与排布格式无关,检查段落设置,插入图片的行应该时被设置了固定行距。 把行高设置为单倍行距即可。...

错误:无法与 SFTP 服务器建立 FTP 连接

错误:无法与 SFTP 服务器建立 FTP 连接

使用filezilla连接远端服务器,报错如下: 错误:无法与 SFTP 服务器建立 FTP 连接 原因:FileZilla 默认使用的是FTP协议 解决:把默认协议改为SFTP...

UG NX的NX TOOLS之签名dll工具目录

UG NX的NX TOOLS之签名dll工具目录

UG NX的NX TOOLS工具,用来为dll签名的工具,实际是cmd命令。 但是要能够正确签名,还需要设置相关的环境。 查看该命令提示符属性,得到信息是 C:\Windows\System32\c...