python如何导出微信公众号文章方法详解
作者:coldplay.xixi 时间:2022-10-01 00:31:27
1.安装wkhtmltopdf
下载地址:https://wkhtmltopdf.org/downloads.html
我测试用的是windows的,下载安装后结果如下
2 编写python 代码导出微信公众号文章
不能直接使用wkhtmltopdf 导出微信公众号文章,导出的文章会缺失图片,所以需要使用 wechatsogou 将微信公众号文章页面抓取,之后将html文本转化为pdf
pip install wechatsogou --upgrade
pip install pdfkit
踩坑!!!,看了很多人的代码,都是一个模板,大家都是抄来抄去,结果还是运行不了,可能是因为依赖包更新的原因,也可能是因为我本地没有配置wkhtmltopdf 的环境变量
import os
import pdfkit
import datetime
import wechatsogou
# 初始化API
ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3)
def url2pdf(url, title, targetPath):
'''
使用pdfkit生成pdf文件
:param url: 文章url
:param title: 文章标题
:param targetPath: 存储pdf文件的路径
'''
try:
content_info = ws_api.get_article_content(url)
except:
return False
# 处理后的html
html = f'''
{title}
{content_info['content_html']}
'''
try:
path_wk="E:/softwareAPP/wkhtmltopdf/bin/wkhtmltopdf.exe";
config=pdfkit.configuration(wkhtmltopdf=path_wk)
pdfkit.from_string(input=html, output_path=targetPath,configuration=config)
except:
# 部分文章标题含特殊字符,不能作为文件名
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" )
# gzh_name = ''
# # 如果不存在目标文件夹就进行创建
# if not os.path.exists(targetPath):
# os.makedirs(targetPath)
# # 将该公众号最近10篇文章信息以字典形式返回
# data = ws_api.get_gzh_article_by_history(gzh_name)
# article_list = data['article']
# for article in article_list:
# url = article['content_url']
# title = article['title']
# url2pdf(url, title, targetPath)
来源:https://www.php.cn/python-tutorials-459324.html
标签:python,导出,微信公众号文章
0
投稿
猜你喜欢
深入理解Python分布式爬虫原理
2023-02-06 14:33:30
Dreamweaver4探谜系列(1)
2010-09-05 21:12:00
php+mysql开发的最简单在线题库(在线做题系统)完整案例
2023-08-21 20:03:14
高手进阶:网页设计中的文字运用
2008-10-05 08:58:00
python中面向对象的注意点概述总结
2023-10-08 09:35:11
文字链接,怎么办?
2008-08-04 13:18:00
在MySQL中使用XML数据—数据格式化
2009-12-29 10:26:00
通过优化CSS代码 减小对系统资源的占用
2010-08-03 12:33:00
Python大数据之从网页上爬取数据的方法详解
2023-10-06 22:10:53
Django基础之Model操作步骤(介绍)
2022-04-12 12:17:50
如何用我的国际域名做虚拟域名?
2010-06-16 09:53:00
PDO::rollBack讲解
2023-07-23 06:52:34
django基础学习之send_mail功能
2023-04-01 00:16:53
python实现超市管理系统(后台管理)
2022-08-17 06:50:22
如何更快更好地调试ASP程序代码?
2009-11-23 20:13:00
MySQL中的字符串模式匹配
2010-03-09 16:30:00
python3实现zabbix告警推送钉钉的示例
2023-01-16 06:59:04
写给应聘页面重构的同学
2009-03-18 11:01:00
Python里的dict和set的背后小秘密
2023-11-20 01:01:11
asp sql去左右空格函数
2008-03-04 17:29:00