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)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“python如何导出微信公众号文章方法详解”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。

