python如何导出微信公众号文章方法详解

作者:coldplay.xixi 时间:2022-10-01 00:31:27 

python如何导出微信公众号文章方法详解

1.安装wkhtmltopdf

下载地址:https://wkhtmltopdf.org/downloads.html

我测试用的是windows的,下载安装后结果如下

python如何导出微信公众号文章方法详解

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中实现迭代器(iterator)的方法示例

    2021-07-12 19:32:41
  • 提高CSS代码的可读性

    2008-05-11 18:59:00
  • 如何用Python将图片转为字符画

    2022-06-15 09:26:06
  • Pytest+Yaml+Excel 接口自动化测试框架的实现示例

    2023-01-07 05:48:17
  • 超链“确认”对话框confirm

    2008-05-16 11:42:00
  • Python对多个sheet表进行整合实例讲解

    2021-01-15 06:18:24
  • 解决mysql报错:Data source rejected establishment of connection, message from server: \\"Too many connectio

    2024-01-13 05:53:57
  • 解析pip安装第三方库但PyCharm中却无法识别的问题及PyCharm安装第三方库的方法教程

    2023-05-17 04:25:43
  • Django CBV类的用法详解

    2022-11-24 20:33:43
  • python中count函数知识点浅析

    2023-05-21 18:41:30
  • IPython 8.0 Python 命令行交互工具

    2022-10-24 09:17:54
  • Python调用服务接口的实例

    2021-06-04 18:22:06
  • PHP之mysql位运算案例讲解

    2023-06-13 06:16:19
  • Python实现图的广度和深度优先路径搜索算法

    2021-09-14 23:50:19
  • Python数据分析之 Matplotlib 散点图绘制

    2021-09-19 07:05:46
  • 使用Python脚本将文字转换为图片的实例分享

    2022-07-20 14:33:29
  • Runnable.com 在线测试代码片分享网站

    2023-02-04 09:25:54
  • 详解Python中的路径问题

    2021-06-05 08:48:45
  • MySQL千万级数据的大表优化解决方案

    2024-01-18 06:24:02
  • 解决vue单页面多个组件嵌套监听浏览器窗口变化问题

    2024-04-27 15:48:29
  • asp之家 网络编程 m.aspxhome.com