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

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

秋风渡红尘8年前 (2018-06-27)技术手札3190
#!/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://www.yiyabei.cn/?id=22

标签: calibre
分享给朋友:

相关文章

设备基础灌浆分类及要求

设备基础灌浆分类及要求

        一、灌浆分类和灌浆料         1.灌浆分类 &...

QT在windows环境下QIcon不显示

QT在windows环境下QIcon不显示

QT设计窗口程序,在linux下QIcon能够正常显示图标,在windoows下无法显示图标。 经检查路径没有问题,最终解决方法是 先构建-˃执行qmake,然后在重新run。 图标终于正常显示,完毕!...

AUTOCAD2018无法启动及启动后界面闪烁

AUTOCAD2018无法启动及启动后界面闪烁

AUTOCAD2018安装完毕后启动时无法进入主界面就报错“autocad application 已停止工作”。 用管理员身份或者兼容模式运行,会在进入主界面之前自动退出程序。 通过visualsutio调试,发现问题出现在nvwgf...

局域网和外网多重网络同时使用

局域网和外网多重网络同时使用

要点: 1、ip、子网掩码都使用内网; 2、网关保证空白; 3、设置静态路由。 具体操作: 1、保证完成了上述1、2两条操作; 2、设置静态路由...

ubuntu VPS 安装LXDE+VNC桌面 附配Firefox浏览器及简体中文

ubuntu VPS 安装LXDE+VNC桌面 附配Firefox浏览器及简体中文

apt-get install xorg lxde-core tightvncserver tightvncserver :1 tightvncserver -kill :1 vi ~/.vnc/xstartup 添加 lxter...