這篇文章將為大家詳細講解有關python導出微信公眾號文章的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)主營湖北網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都app軟件開發(fā)公司,湖北h5成都微信小程序搭建,湖北網(wǎng)站營銷推廣歡迎湖北等地區(qū)企業(yè)咨詢
下載地址:https://wkhtmltopdf.org/downloads.html
我測試用的是windows的,下載安裝后結果如下
不能直接使用wkhtmltopdf 導出微信公眾號文章,導出的文章會缺失圖片,所以需要使用 wechatsogou 將微信公眾號文章頁面抓取,之后將html文本轉(zhuǎn)化為pdf
pip install wechatsogou --upgrade
pip install pdfkit
踩坑!!!,看了很多人的代碼,都是一個模板,大家都是抄來抄去,結果還是運行不了,可能是因為依賴包更新的原因,也可能是因為我本地沒有配置wkhtmltopdf 的環(huán)境變量
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''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{title}</title> </head> <body> <h3 style="text-align: center;font-weight: 400;">{title}</h3> {content_info['content_html']} </body> </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的系統(tǒng)架構全視角解讀","G:/test/hbase文檔.pdf" ) # gzh_name = '' # # 如果不存在目標文件夾就進行創(chuàng)建 # 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)
關于python導出微信公眾號文章的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文名稱:python導出微信公眾號文章的方法
地址分享:http://www.chinadenli.net/article0/ishhoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、網(wǎng)站維護、Google、手機網(wǎng)站建設、App設計、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)