吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3555|回复: 6
收起左侧

[Python 转载] pytho pdfkit 将网页django2.0教程内容打印成pdf文档

  [复制链接]
huguo002 发表于 2019-8-3 14:06
pytho pdfkit 将网页django2.0教程内容打印成pdf文档


最近在学习django2.0,把杜赛大佬的django2.0网页教程打印成pdf文档 方便学习!
https://www.dusaiphoto.com/article/detail/2/
感谢大佬的分享!


附:django2.0教程pdf文档
链接: https://pan.baidu.com/s/1HM7_tEqcPk3h2ffZiYV17Q 提取码: udpi  七天有效期!


11.jpg


22.jpg


33.jpg




代码:
[Python] 纯文本查看 复制代码
#采集网页打印成pdf文档输出
# -*- coding: UTF-8 -*-
import pdfkit
import requests
from lxml import etree
import re

confg = pdfkit.configuration(wkhtmltopdf=r'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\wkhtmltox\bin\wkhtmltopdf.exe')


#获取链接
def get_listurl():
    url="https://www.dusaiphoto.com/article/detail/2/"
    list_url = [url,]
    html=requests.get(url).content.decode('utf-8')
    con=re.findall(r'<div class="card-text" style="overflow: hidden">(.+?)<div class="container-fluid">',html,re.S)[0]
    listurls=re.findall(r'<p class="mb-0">.+?<a href="(.+?)".+?style="color: #b8b8b8;"',con,re.S)
    for listurl in listurls:
        listurl=f'https://www.dusaiphoto.com{listurl}'
        list_url.append(listurl)
    print(list_url)
    return list_url

#获取正文内容
def get_content(url):
    #url='https://www.dusaiphoto.com/article/detail/4/'
    html=requests.get(url).content.decode('utf-8')
    content=re.findall(r'<div class="mt-4">(.+?)<div class="mt-4 mb-4">',html,re.S)[0]
    return content

#保存html为pdf文档
def dypdf(contents):
    contents=etree.HTML(contents)
    s = etree.tostring(contents).decode()
    print("开始打印内容!")
    pdfkit.from_string(s, r'out.pdf',configuration=confg)
    print("打印保存成功!")


if __name__ == '__main__':
    contents=''
    urls=get_listurl()
    for url in urls:
        print(url)
        content=get_content(url)
        contents='%s%s%s'%(contents,content,'<p><br><p>')

    dypdf(contents)



wkhtmltopdf,这个工具的下载网站是:https://wkhtmltopdf.org/downloads.html


代码中,confg = pdfkit.configuration(wkhtmltopdf=r’C:\Users\Administrator\AppData\Local\Programs\Python\Python37\wkhtmltox\bin\wkhtmltopdf.exe’)
wkhtmltopdf=更换为你自己的包目录!


参考以下教程:


使用python把html网页转成pdf文件 https://www.cnblogs.com/xiaowenshu/p/9916719.html


写的比较粗糙,见谅!


查了下论坛 发现另一个老哥的更完善!大家可以参考他的!


Python爬取C语言中文网教程生成PDF
https://www.52pojie.cn/thread-990598-1-1.html


@null119


发帖不易,有帮助的话,麻烦贵手给个热心,给个赞!
有问题欢迎交流!
有在学django的老哥也可以交流一下!


如果有帮助到你!麻烦给个热心值!感谢!




免费评分

参与人数 3吾爱币 +5 热心值 +2 收起 理由
时空之外 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
凯咪 + 1 素质三联支持一波

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lowkey_06 发表于 2019-8-3 14:18
PYTHON 留名
blvan 发表于 2020-8-15 09:40
一山一水山人 发表于 2020-9-1 08:42
pcwuyu 发表于 2020-10-22 10:05
失效啦,有没有新的啊
carrot_52 发表于 2020-10-22 11:06
感谢分享
一山一水山人 发表于 2021-2-6 08:58
谢谢楼主分享,学习了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 21:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表