首頁  >  文章  >  微信小程式  >  爬取微信公眾號文章並儲存為PDF檔案(Python方法)

爬取微信公眾號文章並儲存為PDF檔案(Python方法)

coldplay.xixi
coldplay.xixi轉載
2020-08-29 17:14:485411瀏覽

爬取微信公眾號文章並儲存為PDF檔案(Python方法)

【相關學習推薦:#微信公眾號開發教學

#前言

第一次寫博客,主要內容是爬取微信公眾號的文章,將文章以PDF格式保存在本地。

爬取微信公眾號文章(使用wechatsogou)

1.安裝

pip install wechatsogou --upgrade

wechatsogou是一個基於搜狗微信搜尋的微信公眾號爬蟲介面

2.使用方法

使用方法如下所示

import wechatsogou
# captcha_break_time为验证码输入错误的重试次数,默认为1
ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3)
# 公众号名称
gzh_name = ''
# 将该公众号最近10篇文章信息以字典形式返回
data = ws_api.get_gzh_article_by_history(gzh_name)

data資料結構:

{
    'gzh': {
        'wechat_name': '',  # 名称
        'wechat_id': '',  # 微信id
        'introduction': '',  # 简介
        'authentication': '',  # 认证
        'headimage': ''  # 头像
    },
    'article': [
        {
            'send_id': int,  # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致
            'datetime': int,  # 群发datatime 10位时间戳
            'type': '',  # 消息类型,均是49(在手机端历史消息页有其他类型,网页端最近10条消息页只有49),表示图文
            'main': int,  # 是否是一次群发的第一次消息 1 or 0
            'title': '',  # 文章标题
            'abstract': '',  # 摘要
            'fileid': int,  #
            'content_url': '',  # 文章链接
            'source_url': '',  # 阅读原文的链接
            'cover': '',  # 封面图
            'author': '',  # 作者
            'copyright_stat': int,  # 文章类型,例如:原创啊
        },
        ...
    ]
}

這裡需要得到兩個資訊:文章標題,文章url。

得到文章url以後,就可以依照url將html頁面轉換成pdf檔了。

產生PDF檔案

1.安裝wkhtmltopdf

下載位址:https://wkhtmltopdf.org/downloads.html

2.安裝pdfkit

pip install pdfkit

3.使用方法

import pdfkit
# 根据url生成pdf
pdfkit.from_url('http://baidu.com','out.pdf')
# 根据html文件生成pdf
pdfkit.from_file('test.html','out.pdf')
# 根据html代码生成pdf
pdfkit.from_string('Hello!','out.pdf')

如果直接用上面得到的文章url去產生pdf,會出現pdf檔不顯示文章圖片的問題。

解決方法:<pre class="brush:php;toolbar:false;"># 该方法根据文章url对html进行处理,使图片显示 content_info = ws_api.get_article_content(url) # 得到html代码(代码不完整,需要加入head、body等标签) html_code = content_info[&amp;#39;content_html&amp;#39;]</pre>然後根據html_code建構完整的html程式碼,呼叫

pdfkit.from_string()

方法產生pdf文件,這時候會發現文章中的圖片在pdf檔中顯示出來了。

完整程式碼

import os
import pdfkit
import datetime
import wechatsogou

# 初始化API
ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3)


def url2pdf(url, title, targetPath):
    &#39;&#39;&#39;
    使用pdfkit生成pdf文件
    :param url: 文章url
    :param title: 文章标题
    :param targetPath: 存储pdf文件的路径
    &#39;&#39;&#39;
    try:
        content_info = ws_api.get_article_content(url)
    except:
        return False
    # 处理后的html
    html = f&#39;&#39;&#39;
    <!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:
        pdfkit.from_string(html, targetPath + os.path.sep + f&#39;{title}.pdf&#39;)
    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;:
    # 此处为要爬取公众号的名称
    gzh_name = &#39;&#39;
    targetPath = os.getcwd() + os.path.sep + 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[&#39;article&#39;]
    for article in article_list:
        url = article[&#39;content_url&#39;]
        title = article[&#39;title&#39;]
        url2pdf(url, title, targetPath)
相關學習推薦:

python教學
######

以上是爬取微信公眾號文章並儲存為PDF檔案(Python方法)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除