Home >Backend Development >Python Tutorial >How to export WeChat public account articles in python

How to export WeChat public account articles in python

coldplay.xixi
coldplay.xixiforward
2020-08-29 16:34:183085browse

How to export WeChat public account articles in python

Related learning recommendations: python tutorial

1. Install wkhtmltopdf

Download address: https://wkhtmltopdf.org/downloads.html

I used windows for testing. After downloading and installing, the results are as follows

2 Write python code to export WeChat public account articles

You cannot directly use wkhtmltopdf to export WeChat public account articles. The exported articles will lack pictures, so you need to use wechatsogou to crawl the WeChat public account article page, and then convert the html Convert text to pdf

pip install wechatsogou --upgrade

pip install pdfkit

Step on the trap! ! ! After reading a lot of people's codes, they are all a template. Everyone copied them over and over again, but it still doesn't work. It may be because the dependency package has been updated, or it may be because I have not configured the environment variables of wkhtmltopdf locally

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>
    <h2 style="text-align: center;font-weight: 400;">{title}</h2>
    {content_info[&#39;content_html&#39;]}
    </body>
    </html>
    &#39;&#39;&#39;
    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(&#39;%Y%m%d%H%M%S&#39;) + &#39;.pdf&#39;
        pdfkit.from_string(html, targetPath + os.path.sep + filename)



if __name__ == &#39;__main__&#39;:
    # 此处为要爬取公众号的名称

    url2pdf("https://mp.weixin.qq.com/s/wwT5n2JwEEAkrrmOhedziw", "HBase的系统架构全视角解读","G:/test/hbase文档.pdf" )
    # gzh_name = &#39;&#39;
    # # 如果不存在目标文件夹就进行创建
    # if not os.path.exists(targetPath):
    #     os.makedirs(targetPath)
    # # 将该公众号最近10篇文章信息以字典形式返回
    # data = ws_api.get_gzh_article_by_history(gzh_name)
    # article_list = data[&#39;article&#39;]
    # for article in article_list:
    #     url = article[&#39;content_url&#39;]
    #     title = article[&#39;title&#39;]
    #     url2pdf(url, title, targetPath)

Related learning recommendations: WeChat Mini Program Tutorial

The above is the detailed content of How to export WeChat public account articles in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete