python如何导出微信公众号文章方法详解
1.安装wkhtmltopdf
下载地址:https://wkhtmltopdf.org/downloads.html
我测试用的是windows的,下载安装后结果如下
2编写python代码导出微信公众号文章
不能直接使用wkhtmltopdf导出微信公众号文章,导出的文章会缺失图片,所以需要使用wechatsogou将微信公众号文章页面抓取,之后将html文本转化为pdf
pipinstallwechatsogou--upgrade pipinstallpdfkit
踩坑!!!,看了很多人的代码,都是一个模板,大家都是抄来抄去,结果还是运行不了,可能是因为依赖包更新的原因,也可能是因为我本地没有配置wkhtmltopdf的环境变量
importos
importpdfkit
importdatetime
importwechatsogou
#初始化API
ws_api=wechatsogou.WechatSogouAPI(captcha_break_time=3)
defurl2pdf(url,title,targetPath):
'''
使用pdfkit生成pdf文件
:paramurl:文章url
:paramtitle:文章标题
:paramtargetPath:存储pdf文件的路径
'''
try:
content_info=ws_api.get_article_content(url)
except:
returnFalse
#处理后的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=''
##如果不存在目标文件夹就进行创建
#ifnotos.path.exists(targetPath):
#os.makedirs(targetPath)
##将该公众号最近10篇文章信息以字典形式返回
#data=ws_api.get_gzh_article_by_history(gzh_name)
#article_list=data['article']
#forarticleinarticle_list:
#url=article['content_url']
#title=article['title']
#url2pdf(url,title,targetPath)
到此这篇关于python如何导出微信公众号文章方法详解的文章就介绍到这了,更多相关python导出微信公众号文章内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。