1.安装wkhtmltopdf
下载地址:https://wkhtmltopdf.org/downloads.html
我测试用的是windows的,下载安装后结果如下
![技术分享图片](https://img-blog.csdnimg.cn/20200828173514452.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1bWluZ3podTE=,size_16,color_FFFFFF,t_70)
2 编写python 代码导出微信公众号文章
不能直接使用wkhtmltopdf 导出微信公众号文章,导出的文章会缺失图片,所以需要使用 wechatsogou 将微信公众号文章页面抓取,之后将html文本转化为pdf
pip install wechatsogou --upgrade
pip install pdfkit
踩坑!!!,看了很多人的代码,都是一个模板,大家都是抄来抄去,结果还是运行不了,可能是因为依赖包更新的原因,也可能是因为我本地没有配置wkhtmltopdf 的环境变量
-
-
-
-
-
-
-
ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3)
-
def url2pdf(url, title, targetPath):
-
-
-
-
-
:param targetPath: 存储pdf文件的路径
-
-
-
content_info = ws_api.get_article_content(url)
-
-
-
-
-
-
-
-
-
-
-
-
<h2 style="text-align: center;font-weight: 400;">{title}</h2>
-
{content_info[‘content_html‘]}
-
-
-
-
-
path_wk="E:/softwareAPP/wkhtmltopdf/bin/wkhtmltopdf.exe";
-
config=pdfkit.configuration(wkhtmltopdf=path_wk)
-
pdfkit.from_string(input=html, output_path=targetPath,configuration=config)
-
-
-
-
filename = datetime.datetime.now().strftime(‘%Y%m%d%H%M%S‘) + ‘.pdf‘
-
pdfkit.from_string(html, targetPath + os.path.sep + filename)
-
-
-
-
if __name__ == ‘__main__‘:
-
-
-
url2pdf("https://mp.weixin.qq.com/s/wwT5n2JwEEAkrrmOhedziw", "HBase的系统架构全视角解读","G:/test/hbase文档.pdf" )
-
-
-
-
-
-
-
-
-
-
-
python 使用 wechatsogou wkthmltopdf 导出微信公众号文章
原文:https://www.cnblogs.com/pythonzhilian/p/13584391.html